trivial macro breaks type checking in the compiler #4608

Closed
jxy opened this Issue Aug 11, 2016 · 3 comments

Projects

None yet

2 participants

@jxy
jxy commented Aug 11, 2016

With the following code

import macros
# macro m(n:typed):untyped = n.copy   # Error with Nim version 0.14.2 and the devel branch
macro m(n:typed):untyped = n          # Error with the devel branch but not version 0.14.2
type
  U = object
    v:array[1,float]
  V = object
    v:U
proc `[]`(x:var U,y:int):var float = x.v[y]
template `+=`(x:var V,y:V):untyped = x.v[0]+=y.v[0]
template `+=`(x:V,y:V):untyped = x.v[0]+=y.v[0]
var x,y:V
x+=y          # compiles
m:
  x+=y        # Error: type mismatch: got (var float, system.float)

The devel branch errors with

t_peq_template.nim(10, 44) Error: type mismatch: got (var float, system.float)
but expected one of: 
proc `+=`[T: float | float32 | float64](x: var T; y: T)
for a 'var' type a variable needs to be passed, but 'x.v[0]' is immutable

Note that if you comment out the trivial macro call of m, it compiles. I guess some type checking mechanism in the compiler vm after a call of macro is incorrect.

In addition, if in the macro definition copy is called, the release version 0.14.2 of the compiler also errors with the same error message from the devel branch.

@jxy
jxy commented Oct 1, 2016

I'm still hitting this bug with 0.15.0.

Does anyone have some idea as to where in the compiler source I should poke at?

Or any workaround as to what in the macro I can do to circumvent this issue?

@Araq
Member
Araq commented Oct 1, 2016

Hmm sorry, this should have been marked as 'regression' to get a higher priority.

@Araq Araq added the Regression label Oct 1, 2016
@jxy
jxy commented Oct 14, 2016

Did a bisect, and found out the offending commit, e73fd64, is a relative short patch, but I'm not very familiar with the semantic check. Is there a document about the semantic check part of the compiler? Which part of the code do you recommend for me to start looking at?

@Araq Araq added a commit that closed this issue Oct 18, 2016
@Araq Araq fixes #4608 34b826a
@Araq Araq closed this in 34b826a Oct 18, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment