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

Return type annotations fail to parse on functions with no body #19106

Closed
iamed2 opened this issue Oct 25, 2016 · 6 comments
Closed

Return type annotations fail to parse on functions with no body #19106

iamed2 opened this issue Oct 25, 2016 · 6 comments
Assignees
Labels
bug Indicates an unexpected problem or unintended behavior compiler:lowering Syntax lowering (compiler front end, 2nd stage)

Comments

@iamed2
Copy link
Contributor

iamed2 commented Oct 25, 2016

julia> function func()::Void end
syntax: malformed expression

julia> function func()::Void
       end
syntax: malformed expression

julia> function func()::Int
       end
syntax: malformed expression

julia> versioninfo()
Julia Version 0.5.1-pre+2
Commit f0d40ec* (2016-09-20 03:34 UTC)
Platform Info:
  System: Darwin (x86_64-apple-darwin16.0.0)
  CPU: Intel(R) Core(TM) i7-5557U CPU @ 3.10GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.7.1 (ORCJIT, broadwell)
@ihnorton ihnorton added the compiler:lowering Syntax lowering (compiler front end, 2nd stage) label Oct 26, 2016
@ihnorton
Copy link
Member

This won't do what you want (see #16432 (comment)), but we can give a better error message.

@iamed2
Copy link
Contributor Author

iamed2 commented Oct 26, 2016

I don't think any of these should error on definition. Given:

julia> function g() end
g (generic function with 1 method)

julia> @code_lowered g()
LambdaInfo template for g() at REPL[6]:1
:(begin
        nothing
        return
    end)
julia> function f()::Int
           nothing
       end
f (generic function with 1 method)

julia> @code_lowered f()
LambdaInfo template for f() at REPL[5]:2
:(begin
        nothing
        return (Base.convert)(Main.Int,Main.nothing)
    end)
julia> function h()::Int
           nothing
           return
       end
h (generic function with 1 method)

julia> @code_lowered h()
LambdaInfo template for h() at REPL[9]:2
:(begin
        nothing
        SSAValue(0) = Main.Int
        Main.nothing # line 3:
        return (Core.typeassert)((Base.convert)(SSAValue(0),nothing),SSAValue(0))
    end)

I would expect a call to this function:

function i()::Int
end

to lower to either:

:(begin
        SSAValue(0) = Main.Int
        Main.nothing
        return (Core.typeassert)((Base.convert)(SSAValue(0),nothing),SSAValue(0))
    end)

or

:(begin
        return (Base.convert)(Main.Int,Main.nothing)
    end)

@JeffBezanson JeffBezanson self-assigned this Oct 26, 2016
@JeffBezanson JeffBezanson added the bug Indicates an unexpected problem or unintended behavior label Oct 26, 2016
@ihnorton
Copy link
Member

@JeffBezanson this works, but not sure if there are other cases to handle:

diff --git a/src/julia-syntax.scm b/src/julia-syntax.scm
index 09f6459..0353684 100644
--- a/src/julia-syntax.scm
+++ b/src/julia-syntax.scm
@@ -3380,9 +3380,9 @@ f(x) = yt(x)
                           ;; strip filenames out of non-initial line nodes
                           (emit `(line ,(cadr e)))))
                      ((and (eq? (car e) 'meta) (length> e 2) (eq? (cadr e) 'ret-type))
-                      (assert (not value))
                       (assert (not rett))
-                      (set! rett (caddr e)))
+                      (set! rett (caddr e))
+                      (if (not value) (emit-return rett)))
                      (else
                       (emit e)))
                (if (and tail (not have-ret?))

@JeffBezanson
Copy link
Member

The last line of that patch doesn't make sense; it says to return the value rett itself, i.e. return Void in this case.

JeffBezanson added a commit that referenced this issue Oct 26, 2016
fix #19106, ret type decl on function with empty body
@iamed2
Copy link
Contributor Author

iamed2 commented Oct 26, 2016

Is a backport possible?

@tkelman
Copy link
Contributor

tkelman commented Oct 27, 2016

#19114 is already labeled. Be careful to declare your minimum version dependency as 0.5.1 if you want to rely on this working though.

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 compiler:lowering Syntax lowering (compiler front end, 2nd stage)
Projects
None yet
Development

No branches or pull requests

4 participants