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

Optionally generated branches ignore errors #28971

Closed
cstjean opened this issue Aug 30, 2018 · 2 comments
Closed

Optionally generated branches ignore errors #28971

cstjean opened this issue Aug 30, 2018 · 2 comments

Comments

@cstjean
Copy link
Contributor

cstjean commented Aug 30, 2018

When run interactively, or inside a file,

julia> function foo()
          if @generated
              error("some bug in code generation")
          else
              det(rand(100,100))
          end
      end
foo (generic function with 2 methods)

julia> foo()
3.0930222047

I know - the compiler is allowed to pick whichever branch it wants, so technically there's nothing wrong here. But this behavior (skipping to the alternative branch whenever there's an error in codegen) makes development really tricky, since nothing will suggest that there's anything wrong, assuming that the non-generated branch is correct.

I don't know how Julia decides which branch to run, but my expectation was that it would either run the codegen in the generated branch first (thus triggering the error), or at least run inference on the code-generation code (thus seeing that its output-type is Union{} - in which case it's clear that something's wrong). Could we get an error in those cases?

More generally, it'd be nice to have some guidelines about the compiler's decision process for optionally generated functions, because otherwise it's hard to rely on for performance. Perhaps that's a separate issue though.

@JeffBezanson
Copy link
Member

I think the first thing to do is this patch:

--- a/base/reflection.jl
+++ b/base/reflection.jl
@@ -682,7 +682,7 @@ function code_lowered(@nospecialize(f), @nospecialize(t = Tuple); 
     return map(method_instances(f, t)) do m
         if generated && isgenerated(m)
             if isa(m, Core.MethodInstance)
-                return Core.Compiler.get_staged(m)
+                return ccall(:jl_code_for_staged, Any, (Any,), m)::CodeInfo
             else # isa(m, Method)
                 error("Could not expand generator for `@generated` method ", m, ". ",

Then at least code_lowered will expose such errors when you ask for the generated form.

@cstjean cstjean changed the title Optional generated branches ignore errors Optionally generated branches ignore errors Nov 5, 2018
@maleadt
Copy link
Member

maleadt commented Nov 6, 2018

Dup of #25707

@maleadt maleadt closed this as completed Nov 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants