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

Can't do type creation inside macro when templating is used #9312

Closed
dlfivefifty opened this issue Dec 11, 2014 · 7 comments
Closed

Can't do type creation inside macro when templating is used #9312

dlfivefifty opened this issue Dec 11, 2014 · 7 comments
Labels
bug Indicates an unexpected problem or unintended behavior regression Regression in behavior compared to a previous version

Comments

@dlfivefifty
Copy link
Contributor

The following throws the error "ERROR: error compiling anonymous: type definition not allowed inside a local scope":

module FooModule
     macro foo(Typ,Abs)
     return esc(quote
           abstract $Abs{T}
           immutable $Typ{T} <: $Abs{T} end
           end
           )
       end
    @foo(FooTyp,FooAbs)
end

The following without the templated abstract type doesn't throw an error:

module FooModule2
     macro foo(Typ,Abs)
     return esc(quote
           abstract $Abs
           immutable $Typ{T} <: $Abs end
           end
           )
       end
    @foo(FooTyp,FooAbs)
end

[@ivarne- edited to add quoting]

@vchuravy
Copy link
Member

Which julia version are you using? On 0.3.3 your first example works for me.

@dlfivefifty
Copy link
Contributor Author

This is a bug in 0.4 I think

Sent from my iPhone

On 11 Dec 2014, at 9:55 am, Valentin Churavy notifications@github.com wrote:

Which julia version are you using? On 0.3.3 your first example works for me.


Reply to this email directly or view it on GitHub.

@ivarne ivarne added the regression Regression in behavior compared to a previous version label Dec 11, 2014
@ihnorton ihnorton added the bug Indicates an unexpected problem or unintended behavior label Dec 30, 2014
@ihnorton
Copy link
Member

ihnorton commented Jan 2, 2015

Not yet sure why this changed. See also #2586.

@ihnorton
Copy link
Member

ihnorton commented Jan 2, 2015

@dlfivefifty here is a (possible) work-around:

macro foo(Typ,Abs)
            return esc(quote
                  abstract $Abs{T}
                  immutable $Typ{S} <: $Abs{S} end
                  end
                  )
              end

Since @JeffBezanson seems to be on right now -- if you see this, can you comment on whether this is more than a dupe of #2586 (and #4893)? I looked at the lowering for Sheehan's examples as well as the code below (which works):

julia> macro foo(AbsSup)
           esc(quote abstract $AbsSup{T} end)
       end

julia> @foo(Sup)

julia> macro bar(AbsSub,AbsSup)
           esc(quote immutable $AbsSub{T} <: $AbsSup{T} end end)
       end

julia> @bar(Sub, Sup)

[edit - copied from the wrong REPL]

From what I can tell the difference is that there is a lambda capture in Sheehan's example, and I believe what is happening is that T is being incorrectly (?) classified as a local variable when it should only be a TypeVar for both declarations. (therefore the code goes through the compiler and hits this error message).

@dlfivefifty
Copy link
Contributor Author

That seems to fix the toy example, thanks! I can't test the real example in 0.4 because the code is also hit by #9378

@yuyichao
Copy link
Contributor

yuyichao commented May 5, 2015

Probably related (the "local T declare twice" error)

try
    @eval begin
        abstract A1{T}
        abstract B1{T} <: A1{T}
    end
catch e
    println(e)
end

abstract A2{T}
abstract B2{T} <: A2{T}

try
    @eval begin
        abstract A3{T}
        type B3{T} <: A3{T}
        end
    end
catch e
    println(e)
end

abstract A4{T}
type B4{T} <: A4{T}
end

Output

ErrorException("syntax: local \"T\" declared twice")
ErrorException("error compiling anonymous: type definition not allowed inside a local scope")

@JeffBezanson
Copy link
Member

Just tried this and it seems to be fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior regression Regression in behavior compared to a previous version
Projects
None yet
Development

No branches or pull requests

6 participants