Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed injects into child classes #2841

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions script/vm/compiler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,26 @@ local function searchFieldByLocalID(source, key, pushResult)
if not fields then
return
end

--Exact classes do not allow injected fields
if source.type ~= 'variable' and source.bindDocs then
---@cast source parser.object
local uri = guide.getUri(source)
for _, src in ipairs(fields) do
if src.type == "setfield" then
local class = vm.getDefinedClass(uri, source)
if class then
for _, doc in ipairs(class:getSets(uri)) do
if vm.docHasAttr(doc, 'exact') then
return
end
end
end
end
end
Comment on lines +109 to +120
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The forloop here seems can early break 🤔

  1. local class = vm.getDefinedClass(uri, source) doesn't depend on src, so it is a constant in this loop
  2. Now whether class is nil or not, it is unrelated to outer loop, and checking the whole logic once is enough:
    • if the class has exact attribute => early return
    • if the class is nil or has no exact attribute => early break
        for _, src in ipairs(fields) do
            if src.type == "setfield" then
                local class = vm.getDefinedClass(uri, source)
                if class then
                    for _, doc in ipairs(class:getSets(uri)) do
                        if vm.docHasAttr(doc, 'exact') then
                            return
                        end
                    end
                end
                break  <-- the break here, because the above logic only need to check once
            end
        end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, that's true, thanks. I will revisit this code (it's been a few months since I wrote that) and make a follow-up PR for that later.

end


local hasMarkDoc = {}
for _, src in ipairs(fields) do
if src.bindDocs then
Expand Down
Loading