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

custom pragmas: correct error condition, remove outdated symkind whitelist #21653

Merged
merged 5 commits into from
Apr 13, 2023

Conversation

metagn
Copy link
Collaborator

@metagn metagn commented Apr 12, 2023

fixes #21652

Along with fixing the error message, instead of using a whitelist of symbol kinds for custom pragmas, use a blacklist of ones that do not work, so we don't have to keep maintaining this list as much (a symbol kind that does not support custom pragmas silently accepts it in the worst case). Currently this is enum fields, for loop variables (both because they do not retain pragma AST after semcheck) and module symbols (as it's not handled). We can also whitelist PragmaApplicableSymKinds - {skEnumField, skForVar, skModule} instead to make it more restrictive, but again in the worst case with the blacklist it is just silently accepted.

Description for previous patch in this PR

The symbol kinds you could previously not apply custom pragmas to were:

skModule
skMacro
skTemplate
skEnumField
skForVar

probably cannot attach pragmas to but still not checked for:
skGenericParam, skResult
skTemp, skUnknown, skConditional, skDynLib, skLabel, skStub, skPackage

Now trying to apply a nonexistant pragma to these symbols will give a proper "invalid pragma" error instead of the "cannot attach custom pragma" error.

This way of doing it will attempt to "attach" existing custom pragmas to these symbol kinds, which should not be a problem for macros and templates, but just swallow the custom pragmas without erroring likely as semchecking removes the pragma AST down the line.

import macros

template prag {.pragma.}

type Foo = enum
  a {.prag.}

echo hasCustomPragma(a, prag) # false
for b {.prag.} in [123]:
  echo hasCustomPragma(b, prag) # false

template c(x: int): int {.prag.} = x
echo hasCustomPragma(c, prag) # true

macro d(x: int): int {.prag.} = x
echo hasCustomPragma(d, prag) # true

# this just has no way of working
{.prag.}
echo hasCustomPragma(modulename, prag) # false

We don't have to fix #21652 this way, there is an easy patch of delaying the "cannot attach custom pragma" error until we succeed in finding the custom pragma in semCustomPragma, but I want to test this "swallowing" behavior against CI first to see if it breaks anything.

@metagn metagn changed the title test not restricting custom pragma applied symbols only give illegal custom pragma error on actual custom pragma Apr 12, 2023
elif sym != nil:
illegalCustomPragma(c, it, sym)
else:
invalidPragma(c, it)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This branch was also never called as sym != nil is guaranteed to be true

So this doesn't change general performance either

@metagn metagn changed the title only give illegal custom pragma error on actual custom pragma custom pragmas: correct error condition, remove outdated symkind whitelist Apr 13, 2023
@Araq Araq merged commit c694d8e into nim-lang:devel Apr 13, 2023
@github-actions
Copy link
Contributor

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

Hint: mm: orc; opt: speed; options: -d:release
166802 lines; 8.735s; 612.59MiB peakmem

capocasa pushed a commit to capocasa/Nim that referenced this pull request May 15, 2023
…elist (nim-lang#21653)

* test not restricting custom pragma applied symbols

fixes nim-lang#21652

* fix other test

* different patch

* fix tests

* actually test nim-lang#18212 and other routines
capocasa pushed a commit to capocasa/Nim that referenced this pull request May 16, 2023
…elist (nim-lang#21653)

* test not restricting custom pragma applied symbols

fixes nim-lang#21652

* fix other test

* different patch

* fix tests

* actually test nim-lang#18212 and other routines
bung87 pushed a commit to bung87/Nim that referenced this pull request Jul 29, 2023
…elist (nim-lang#21653)

* test not restricting custom pragma applied symbols

fixes nim-lang#21652

* fix other test

* different patch

* fix tests

* actually test nim-lang#18212 and other routines
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Confusing error message
2 participants