Skip to content

Commit 8141750

Browse files
authored
Merge pull request #32674 from JuliaLang/vc/pure
fix propagation of pure and const results
2 parents a8a567e + 3e5ffb9 commit 8141750

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

base/compiler/abstractinterpretation.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,8 @@ function typeinf_local(frame::InferenceState)
11471147
# only propagate information we know we can store
11481148
# and is valid inter-procedurally
11491149
rt = widenconst(rt)
1150+
elseif isa(rt, Const) && rt.actual
1151+
rt = Const(rt.val)
11501152
end
11511153
if tchanged(rt, frame.bestguess)
11521154
# new (wider) return type for frame

base/compiler/ssair/inlining.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ function analyze_method!(idx::Int, sig::Signature, @nospecialize(metharg), meths
663663
methsig = method.sig
664664

665665
# Check whether this call just evaluates to a constant
666-
if isa(f, widenconst(ft)) && !isdefined(method, :generator) && method.pure &&
666+
if isa(f, widenconst(ft)) &&
667667
isa(stmttyp, Const) && stmttyp.actual && is_inlineable_constant(stmttyp.val)
668668
return ConstantCase(quoted(stmttyp.val), method, Any[methsp...], metharg)
669669
end

test/compiler/contextual.jl

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ module MiniCassette
6262
end
6363

6464
function overdub_generator(self, c, f, args)
65-
if f <: Core.Builtin || !isdefined(f, :instance)
65+
if !isdefined(f, :instance)
6666
return :(return f(args...))
6767
end
6868

@@ -83,6 +83,10 @@ module MiniCassette
8383
code_info
8484
end
8585

86+
@inline function overdub(c::Ctx, f::Union{Core.Builtin, Core.IntrinsicFunction}, args...)
87+
f(args...)
88+
end
89+
8690
@eval function overdub(c::Ctx, f, args...)
8791
$(Expr(:meta, :generated_only))
8892
$(Expr(:meta,
@@ -112,5 +116,20 @@ f() = 2
112116
# Test that pure propagates for Cassette
113117
Base.@pure isbitstype(T) = T.isbitstype
114118
f31012(T) = Val(isbitstype(T))
115-
@test @inferred overdub(Ctx(), f31012, Int64) == Val(true)
119+
@test @inferred(overdub(Ctx(), f31012, Int64)) == Val(true)
120+
121+
@generated bar(::Val{align}) where {align} = :(42)
122+
foo(i) = i+bar(Val(1))
116123

124+
@test @inferred(overdub(Ctx(), foo, 1)) == 43
125+
126+
# Check that misbehaving pure functions propagate their error
127+
Base.@pure func1() = 42
128+
Base.@pure func2() = (this_is_an_exception; func1())
129+
130+
let method = which(func2, ())
131+
mi = Core.Compiler.specialize_method(method, Tuple{typeof(func2)}, Core.svec())
132+
mi.inInference = true
133+
end
134+
func3() = func2()
135+
@test_throws UndefVarError func3()

0 commit comments

Comments
 (0)