Skip to content

fix propagation of pure and const results #32674

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

Merged
merged 1 commit into from
Jul 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions base/compiler/abstractinterpretation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,8 @@ function typeinf_local(frame::InferenceState)
# only propagate information we know we can store
# and is valid inter-procedurally
rt = widenconst(rt)
elseif isa(rt, Const) && rt.actual
rt = Const(rt.val)
end
if tchanged(rt, frame.bestguess)
# new (wider) return type for frame
Expand Down
2 changes: 1 addition & 1 deletion base/compiler/ssair/inlining.jl
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ function analyze_method!(idx::Int, sig::Signature, @nospecialize(metharg), meths
methsig = method.sig

# Check whether this call just evaluates to a constant
if isa(f, widenconst(ft)) && !isdefined(method, :generator) && method.pure &&
if isa(f, widenconst(ft)) &&
isa(stmttyp, Const) && stmttyp.actual && is_inlineable_constant(stmttyp.val)
return ConstantCase(quoted(stmttyp.val), method, Any[methsp...], metharg)
end
Expand Down
23 changes: 21 additions & 2 deletions test/compiler/contextual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ module MiniCassette
end

function overdub_generator(self, c, f, args)
if f <: Core.Builtin || !isdefined(f, :instance)
if !isdefined(f, :instance)
return :(return f(args...))
end

Expand All @@ -83,6 +83,10 @@ module MiniCassette
code_info
end

@inline function overdub(c::Ctx, f::Union{Core.Builtin, Core.IntrinsicFunction}, args...)
f(args...)
end

@eval function overdub(c::Ctx, f, args...)
$(Expr(:meta, :generated_only))
$(Expr(:meta,
Expand Down Expand Up @@ -112,5 +116,20 @@ f() = 2
# Test that pure propagates for Cassette
Base.@pure isbitstype(T) = T.isbitstype
f31012(T) = Val(isbitstype(T))
@test @inferred overdub(Ctx(), f31012, Int64) == Val(true)
@test @inferred(overdub(Ctx(), f31012, Int64)) == Val(true)

@generated bar(::Val{align}) where {align} = :(42)
foo(i) = i+bar(Val(1))

@test @inferred(overdub(Ctx(), foo, 1)) == 43

# Check that misbehaving pure functions propagate their error
Base.@pure func1() = 42
Base.@pure func2() = (this_is_an_exception; func1())

let method = which(func2, ())
mi = Core.Compiler.specialize_method(method, Tuple{typeof(func2)}, Core.svec())
mi.inInference = true
end
func3() = func2()
@test_throws UndefVarError func3()