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

Nim tries to resolve generic field type name even though when condition is false #4774

Closed
endragor opened this issue Sep 15, 2016 · 5 comments · Fixed by #24119
Closed

Nim tries to resolve generic field type name even though when condition is false #4774

endragor opened this issue Sep 15, 2016 · 5 comments · Fixed by #24119

Comments

@endragor
Copy link
Contributor

endragor commented Sep 15, 2016

Sample:

const hasThreadSupport = compileOption("threads")

type
  SomeObj*[T] = object
    when hasThreadSupport:
      channel: ptr Channel[T]
      # Error: undeclared identifier: 'Channel'

The error occurs when you try to compile this without --threads:on option.
That is only specific to generic types.

Nim Git hash: 9784e36

@dom96
Copy link
Contributor

dom96 commented Sep 19, 2016

Hrm, could this be the same as #4797?

@endragor
Copy link
Contributor Author

Yeah, looks the same.

@nc-x
Copy link
Contributor

nc-x commented Dec 31, 2018

The reason is that in generic contexts, we do not set the correct branch to be evaluated and instead evaluate all the branches

Nim/compiler/semtypes.nim

Lines 663 to 668 in ab72d68

if c.inGenericContext == 0:
var e = semConstBoolExpr(c, it.sons[0])
if e.kind != nkIntLit: internalError(c.config, e.info, "semRecordNodeAux")
elif e.intVal != 0 and branch == nil: branch = it.sons[1]
else:
it.sons[0] = forceBool(c, semExprWithType(c, it.sons[0]))

It can be fixed currently for simple boolean expressions I think.
But it cannot be fixed currently for expressions like when T is SomeType.
Because semIs does not evaluate the expression at that moment so we cannot know which branch to take

Nim/compiler/semexprs.nim

Lines 407 to 409 in ab72d68

if c.inGenericContext > 0 and lhsType.base.containsGenericType:
# BUGFIX: don't evaluate this too early: ``T is void``
return

IDK what can be done here.
The only possibility I think is to move the check to instantiation time for generic objects.

@zah zah added the Generics label Jan 2, 2019
@Araq Araq removed the Severe label Feb 9, 2019
@Araq
Copy link
Member

Araq commented Feb 9, 2019

This is really nasty to fix...

@metagn metagn self-assigned this Aug 25, 2024
@metagn
Copy link
Collaborator

metagn commented Sep 2, 2024

Was fixed in #24042

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants