Skip to content

Conversation

Vindaar
Copy link
Contributor

@Vindaar Vindaar commented Jun 10, 2018

This is my first PR for the compiler, so I hope I'm not doing anything weird here...

It fixes issue #7997, which was caused by an export of a release proc
in locks. Thus the release in defined(release) of the ifDebug
template, was of kind nkSym instead of nkIdent.

This PR fixes that by casing on the kind of n.sons[1] in semexpr.semDefined now. If
it's of kind nkSym we get the corresponding PIdent from the symbol
and check if that exists.

The localError is removed, due to a clear distinction between
declared and defined now.

if isDefined(c.config, n.sons[1].sym.name.s):
result.intVal = 1
else:
discard
Copy link
Contributor

Choose a reason for hiding this comment

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

what if someone type something like defined(123), wouldn't it better to tell the user that it is not allowed rather than let it go silently?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

With the change @Araq proposed, that is taken care of, if I'm not mistaken. :)

localError(c.config, n.info, "obsolete usage of 'defined', use 'declared' instead")
elif isDefined(c.config, n.sons[1].ident.s):
result.intVal = 1
case n.sons[1].kind
Copy link
Member

Choose a reason for hiding this comment

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

it's easier than this. Use:

let d = considerQuotedIdent(c.config, n[1], n)
result.intVal = isDefined(c.config, d.s)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oohhh, that's nice! So many helpful procs in the compiler :)

@Araq
Copy link
Member

Araq commented Jun 10, 2018

Oh and please add a test case.

@Vindaar
Copy link
Contributor Author

Vindaar commented Jun 10, 2018

I put the test case under templates. Not sure if that's a good place for it? And I hope I covered all aspects of it.

@@ -0,0 +1,46 @@
discard """
output: Valid and not defined
Copy link
Member

Choose a reason for hiding this comment

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

You need quotes around this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ooops, fixed.

Copy link
Member

Choose a reason for hiding this comment

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

't7997' is a bad test name. Name it 'tdefined' or similiar.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@Vindaar Vindaar force-pushed the fixes-7997 branch 2 times, most recently from e60bf5f to 4d38ff9 Compare June 10, 2018 15:20
elif isDefined(c.config, n.sons[1].ident.s):
result.intVal = 1
let d = considerQuotedIdent(c.config, n[1], n)
result.intVal = if isDefined(c.config, d.s): 1 else: 0
Copy link
Member

Choose a reason for hiding this comment

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

ord isDefined(c.config, d.s) would be more idiomatic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Had no idea that works, hehe. Nice :)

Vindaar added 2 commits June 10, 2018 18:01
Fixes issue nim-lang#7997, which was caused by an export of a `release` proc
in `locks`. Thus the `release` in `defined(release)` of the `ifDebug`
template, was of kind `nkSym` instead of `nkIdent`.

We fix this by getting the `PIdent` of the argument to `defined` using
`considerQuotedIdent`.
This has the nice property of also checking for a valid identifier for
us. E.g. `defined(123)` would fail with
```
Error: in expression 'defined(123)': identifier expected, but found
'123'
```

The `localError` is removed, due to a clear distinction between
`declared` and `defined` now.
@Araq Araq merged commit df1784d into nim-lang:devel Jun 10, 2018
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.

3 participants