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

eval'd expressions inside begin..end should be top-level when possible #2586

Open
mlubin opened this issue Mar 16, 2013 · 9 comments
Open

eval'd expressions inside begin..end should be top-level when possible #2586

mlubin opened this issue Mar 16, 2013 · 9 comments
Assignees
Labels
compiler:interpreter compiler:lowering Syntax lowering (compiler front end, 2nd stage)

Comments

@mlubin
Copy link
Member

mlubin commented Mar 16, 2013

Looks like a bug in eval:

   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: http://docs.julialang.org
   _ _   _| |_  __ _   |  Type "help()" to list help topics
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.2.0-514.r3d43
 _/ |\__'_|_|_|\__'_|  |  Commit 3d43df9032 2013-03-16 06:59:07
|__/                   |

julia> @eval begin
           type T 
               x 
           end
           T(sum([1,2]))
       end
T(3)

julia> @eval begin
           type T2 
               x 
           end
           s = 0
           for i in [1,2]
               s += i
           end
           T2(s)
       end
ERROR: error compiling anonymous: unsupported or misplaced expression composite_type in function anonymous
@JeffBezanson
Copy link
Sponsor Member

You can put let ... end around the for loop as a workaround.

@ghost ghost assigned JeffBezanson Mar 16, 2013
@mlubin
Copy link
Member Author

mlubin commented Mar 16, 2013

That works, thanks.

@mlubin
Copy link
Member Author

mlubin commented Mar 16, 2013

It seems the issue is a bit more fundamental:

julia> function f2()
           type T
               x
           end
           for i in 1:2
               println(i)
           end
       end
# methods for generic function f2
f2() at none:2

julia> f2()
ERROR: error compiling f2: unsupported or misplaced expression composite_type in function f2

I changed the title. I couldn't find any open issues for this.

@mlubin
Copy link
Member Author

mlubin commented Mar 17, 2013

@JeffBezanson, can you clarify if this is a bug or a "feature"? I'll close the issue if it's the latter.

@JeffBezanson
Copy link
Sponsor Member

It's neither a bug nor a feature; it's just that there are no "local types". Types have to be defined at the top level. In the original issue, there are no explicit scopes introduced around the type definition, so it's reasonable to expect that it's at the top level.

@mlubin
Copy link
Member Author

mlubin commented Mar 17, 2013

Got it. The error message for attempting to define types inside functions could also be improved.

@KristofferC
Copy link
Sponsor Member

Original example works and error message now is

syntax: "struct" expression not at top level

@JeffBezanson
Copy link
Sponsor Member

You can still reproduce this if the top-level block contains a ccall (since that forces us to compile it).

@StefanKarpinski
Copy link
Sponsor Member

Should we reopen then?

@KristofferC KristofferC reopened this Apr 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler:interpreter compiler:lowering Syntax lowering (compiler front end, 2nd stage)
Projects
None yet
Development

No branches or pull requests

5 participants