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

fixes #1027; disallow templates to use ambiguous identifiers #20631

Merged
merged 6 commits into from
Oct 24, 2022
Merged

Conversation

ringabout
Copy link
Member

@ringabout ringabout commented Oct 23, 2022

fixes #1027

@ringabout ringabout changed the title test qualifiedLookUp in templates fixes #1027; disallow templates to use ambiguous identifiers Oct 24, 2022
@ringabout ringabout marked this pull request as ready for review October 24, 2022 05:26
@bung87
Copy link
Collaborator

bung87 commented Oct 24, 2022

doesn't this root cause is by ?

let candidates = searchInScopesFilterBy(c, ident, allExceptModule) #.skipAlias(n, c.config)
if candidates.len > 0:
  result = candidates[0]
  amb = candidates.len > 1
  if amb and checkAmbiguity in flags:
    errorUseQualifier(c, n.info, candidates) 

after latest refactoring of searchInScopesFilterBy, candidates.len > 1 never will be true, which means checkAmbiguity become useless.

@ringabout
Copy link
Member Author

after latest refactoring of searchInScopesFilterBy, candidates.len > 1 never will be true, which means checkAmbiguity become useless.

Where is it?

@bung87
Copy link
Collaborator

bung87 commented Oct 24, 2022

after latest refactoring of searchInScopesFilterBy, candidates.len > 1 never will be true, which means checkAmbiguity become useless.

Where is it?

8f4bdb3

@ringabout
Copy link
Member Author

8f4bdb3

I don't think it is related. That PR is regarding shadowing variables.

@bung87
Copy link
Collaborator

bung87 commented Oct 24, 2022

8f4bdb3

I don't think it is related. That PR is regarding shadowing variables.

really? so how can candidates.len > 1 become true ?

@ringabout
Copy link
Member Author

ringabout commented Oct 24, 2022

My PR 8f4bdb3 didn't change the correctness but the performance, two pieces of code are essentially identical.

@bung87
Copy link
Collaborator

bung87 commented Oct 24, 2022

so maybe it doesn't work properly in previous ?

@Varriount Varriount requested a review from Araq October 24, 2022 16:59
@Varriount Varriount added the Requires Araq To Merge PR should only be merged by Araq label Oct 24, 2022
Araq and others added 2 commits October 24, 2022 21:43
Co-authored-by: Clay Sweetser <Varriount@users.noreply.github.com>
Co-authored-by: Clay Sweetser <Varriount@users.noreply.github.com>
@Araq Araq merged commit 064b72a into devel Oct 24, 2022
@Araq Araq deleted the pr_qulify branch October 24, 2022 19:44
@github-actions
Copy link
Contributor

Thanks for your hard work on this PR!
The lines below are statistics of the Nim compiler built from 064b72a

Hint: mm: orc; opt: speed; options: -d:release
164033 lines; 7.958s; 613.152MiB peakmem

@tersec
Copy link
Contributor

tersec commented Jan 3, 2023

Would be useful to have this backported to 1.6 -- it silently generates invalid code.

@arnetheduck
Copy link
Contributor

cc @narimiran

ringabout added a commit that referenced this pull request Feb 20, 2023
* test qualifiedLookUp in templates

* check later

* add testcase

* add 4errormsg

* Update tests/template/m1027a.nim

Co-authored-by: Clay Sweetser <Varriount@users.noreply.github.com>

* Update tests/template/m1027b.nim

Co-authored-by: Clay Sweetser <Varriount@users.noreply.github.com>

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
Co-authored-by: Clay Sweetser <Varriount@users.noreply.github.com>
ringabout added a commit that referenced this pull request Feb 20, 2023
* Add `nkFastAsgn` into `semExpr` (#20939)

* Add nkFastAsgn into case statement

* Add test case

* fixes #1027; disallow templates to use ambiguous identifiers (#20631)

* test qualifiedLookUp in templates

* check later

* add testcase

* add 4errormsg

* Update tests/template/m1027a.nim

Co-authored-by: Clay Sweetser <Varriount@users.noreply.github.com>

* Update tests/template/m1027b.nim

Co-authored-by: Clay Sweetser <Varriount@users.noreply.github.com>

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
Co-authored-by: Clay Sweetser <Varriount@users.noreply.github.com>

---------

Co-authored-by: Jake Leahy <jake@leahy.dev>
Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
Co-authored-by: Clay Sweetser <Varriount@users.noreply.github.com>
capocasa pushed a commit to capocasa/Nim that referenced this pull request Mar 31, 2023
…im-lang#20631)

* test qualifiedLookUp in templates

* check later

* add testcase

* add 4errormsg

* Update tests/template/m1027a.nim

Co-authored-by: Clay Sweetser <Varriount@users.noreply.github.com>

* Update tests/template/m1027b.nim

Co-authored-by: Clay Sweetser <Varriount@users.noreply.github.com>

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
Co-authored-by: Clay Sweetser <Varriount@users.noreply.github.com>
bung87 pushed a commit to bung87/Nim that referenced this pull request Jul 29, 2023
…im-lang#20631)

* test qualifiedLookUp in templates

* check later

* add testcase

* add 4errormsg

* Update tests/template/m1027a.nim

Co-authored-by: Clay Sweetser <Varriount@users.noreply.github.com>

* Update tests/template/m1027b.nim

Co-authored-by: Clay Sweetser <Varriount@users.noreply.github.com>

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
Co-authored-by: Clay Sweetser <Varriount@users.noreply.github.com>
Araq pushed a commit that referenced this pull request Aug 25, 2024
…le types in symchoices (#23997)

fixes #23898, supersedes #23966 and #23990

Since #20631 ambiguous type symbols in templates are rejected outright,
now we generate a symchoice for type nodes if they're ambiguous, a
generalization of what was done in #22375. This is done for generics as
well. Symchoices also handle type symbols better now, ensuring their
type is a `typedesc` type; this probably isn't necessary for everything
to work but it makes the logic more robust.

Similar to #23989, we have to prepare for the fact that ambiguous type
symbols behave differently than normal type symbols and either error
normally or relegate to other routine symbols if the symbol is being
called. Generating a symchoice emulates this behavior, `semExpr` will
find the type symbol first, but since the symchoice has other symbols,
it will count as an ambiguous type symbol.

I know it seems spammy to carry around an ambiguity flag everywhere, but
in the future when we have something like #23104 we could just always
generate a symchoice, and the symchoice itself would carry the info of
whether the first symbol was ambiguous. But this could harm compiler
performance/memory use, it might be better to generate it only when we
have to, which in the case of type symbols is only when they're
ambiguous.
narimiran pushed a commit that referenced this pull request Sep 16, 2024
…le types in symchoices (#23997)

fixes #23898, supersedes #23966 and #23990

Since #20631 ambiguous type symbols in templates are rejected outright,
now we generate a symchoice for type nodes if they're ambiguous, a
generalization of what was done in #22375. This is done for generics as
well. Symchoices also handle type symbols better now, ensuring their
type is a `typedesc` type; this probably isn't necessary for everything
to work but it makes the logic more robust.

Similar to #23989, we have to prepare for the fact that ambiguous type
symbols behave differently than normal type symbols and either error
normally or relegate to other routine symbols if the symbol is being
called. Generating a symchoice emulates this behavior, `semExpr` will
find the type symbol first, but since the symchoice has other symbols,
it will count as an ambiguous type symbol.

I know it seems spammy to carry around an ambiguity flag everywhere, but
in the future when we have something like #23104 we could just always
generate a symchoice, and the symchoice itself would carry the info of
whether the first symbol was ambiguous. But this could harm compiler
performance/memory use, it might be better to generate it only when we
have to, which in the case of type symbols is only when they're
ambiguous.

(cherry picked from commit 09dcff7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Requires Araq To Merge PR should only be merged by Araq
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Templates allowed to use ambiguous identifier
6 participants