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

[SR-11198] Compiler crash on inout-to-pointer used with @autoclosure param returning optional #53595

Closed
hamishknight opened this issue Jul 24, 2019 · 13 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software good first issue Good for newcomers

Comments

@hamishknight
Copy link
Collaborator

Previous ID SR-11198
Radar rdar://problem/53532404
Original Reporter @hamishknight
Type Bug
Status Closed
Resolution Done
Environment

Swift version 5.1-dev (LLVM 200186e28b, Swift 12fa06a)
Target: x86_64-apple-darwin18.5.0

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, CompilerCrash, StarterBug
Assignee @hamishknight
Priority Medium

md5: 6b471bed87673773b6da077848441e95

Issue Description:

The following hits an assertion in SILGen:

func foo(_ x: @autoclosure () -> UnsafePointer<Int>?) {}

var i = 0
foo(&i)
Assertion failed: (isInFormalEvaluationScope() && "must be in a formal evaluation scope"), function emitLValue, file /Users/hamishknight/Desktop/swift-dev/swift/lib/SILGen/SILGenLValue.cpp, line 2309.
Stack dump:
0.  Program arguments: /Users/hamishknight/Desktop/swift-dev/build/Ninja-ReleaseAssert/swift-macosx-x86_64/bin/swift -frontend -emit-silgen -primary-file /Users/hamishknight/Desktop/Stochastic Projects/newnew/newnew/main.swift -target x86_64-apple-darwin18.5.0 -enable-objc-interop -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -color-diagnostics -module-name main -o -
1.  Swift version 5.1-dev (LLVM 200186e28b, Swift 12fa06a4d4)
2.  While silgen closureexpr SIL function "@$s4mainSPySiGSgyXEfu_".
 for expression at [/Users/hamishknight/Desktop/Stochastic Projects/newnew/newnew/main.swift:269:5 - line:269:6] RangeText="&"
0  swift                    0x0000000106905f35 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 37
1  swift                    0x0000000106904f38 llvm::sys::RunSignalHandlers() + 248
2  swift                    0x0000000106906538 SignalHandler(int) + 264
3  libsystem_platform.dylib 0x00007fff7dac8b5d _sigtramp + 29
4  swift                    0x0000000107f7e168 cmark_strbuf__initbuf + 133235
5  libsystem_c.dylib        0x00007fff7d9886a6 abort + 127
6  libsystem_c.dylib        0x00007fff7d95120d basename_r + 0
7  swift                    0x0000000106a7ed93 swift::Lowering::SILGenFunction::emitLValue(swift::Expr*, swift::Lowering::SGFAccessKind, swift::Lowering::LValueOptions) (.cold.1) + 35
8  swift                    0x0000000102f4ad78 swift::Lowering::SILGenFunction::emitLValue(swift::Expr*, swift::Lowering::SGFAccessKind, swift::Lowering::LValueOptions) + 216
9  swift                    0x0000000102f28b45 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 15509
10 swift                    0x0000000102f1aae6 swift::Lowering::SILGenFunction::emitRValueAsSingleValue(swift::Expr*, swift::Lowering::SGFContext) + 54
11 swift                    0x0000000102f01ddb swift::Lowering::SILGenFunction::emitOptionalSome(swift::SILLocation, swift::SILType, llvm::function_ref<swift::Lowering::ManagedValue (swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::SGFContext)>, swift::Lowering::SGFContext) + 491
12 swift                    0x0000000102f29b4e swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 19614
13 swift                    0x0000000102f18040 swift::Lowering::SILGenFunction::emitRValue(swift::Expr*, swift::Lowering::SGFContext) + 32
14 swift                    0x0000000102f8c14b swift::Lowering::SILGenFunction::emitReturnExpr(swift::SILLocation, swift::Expr*) + 379
15 swift                    0x0000000102f3ddf2 swift::Lowering::SILGenFunction::emitClosure(swift::AbstractClosureExpr*) + 370
16 swift                    0x0000000102ebd9e6 swift::Lowering::SILGenModule::emitClosure(swift::AbstractClosureExpr*) + 278
17 swift                    0x0000000102f32e48 (anonymous namespace)::RValueEmitter::visitAbstractClosureExpr(swift::AbstractClosureExpr*, swift::Lowering::SGFContext) + 40
18 swift                    0x0000000102f24f35 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 133
19 swift                    0x0000000102f1aae6 swift::Lowering::SILGenFunction::emitRValueAsSingleValue(swift::Expr*, swift::Lowering::SGFContext) + 54
20 swift                    0x0000000102ed95f8 (anonymous namespace)::ArgEmitter::emit(swift::Lowering::ArgumentSource&&, swift::Lowering::AbstractionPattern) + 1960
21 swift                    0x0000000102ec7dd6 (anonymous namespace)::ArgEmitter::emitSingleArg(swift::Lowering::ArgumentSource&&, swift::Lowering::AbstractionPattern) + 70
22 swift                    0x0000000102ee37c3 (anonymous namespace)::ArgEmitter::emitPreparedArgs(swift::Lowering::PreparedArguments&&, swift::Lowering::AbstractionPattern) + 147
23 swift                    0x0000000102ee357c (anonymous namespace)::CallSite::emit(swift::Lowering::SILGenFunction&, swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::SILFunctionType>, (anonymous namespace)::ParamLowering&, llvm::SmallVectorImpl<swift::Lowering::ManagedValue>&, llvm::SmallVectorImpl<(anonymous namespace)::DelayedArgument>&, llvm::Optional<swift::ForeignErrorConvention> const&, swift::ImportAsMemberStatus) && + 684
24 swift                    0x0000000102ee2f1b (anonymous namespace)::CallEmission::emitArgumentsForNormalApply(swift::CanTypeWrapper<swift::FunctionType>&, swift::Lowering::AbstractionPattern&, swift::CanTypeWrapper<swift::SILFunctionType>, llvm::Optional<swift::ForeignErrorConvention> const&, swift::ImportAsMemberStatus, llvm::SmallVectorImpl<swift::Lowering::ManagedValue>&, llvm::Optional<swift::SILLocation>&, swift::CanTypeWrapper<swift::FunctionType>&) + 1675
25 swift                    0x0000000102ecad54 (anonymous namespace)::CallEmission::apply(swift::Lowering::SGFContext) + 2948
26 swift                    0x0000000102eca134 swift::Lowering::SILGenFunction::emitApplyExpr(swift::ApplyExpr*, swift::Lowering::SGFContext) + 2404
27 swift                    0x0000000102f24f07 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 87
28 swift                    0x0000000102f1afe1 swift::Lowering::SILGenFunction::emitIgnoredExpr(swift::Expr*) + 1137
29 swift                    0x0000000102ec0c14 swift::Lowering::SILGenModule::visitTopLevelCodeDecl(swift::TopLevelCodeDecl*) + 580
30 swift                    0x0000000102ec10e6 swift::Lowering::SILGenModule::emitSourceFile(swift::SourceFile*) + 806
31 swift                    0x0000000102ec205f swift::SILModule::constructSIL(swift::ModuleDecl*, swift::SILOptions&, swift::FileUnit*) + 287
32 swift                    0x0000000102ec2496 swift::performSILGeneration(swift::FileUnit&, swift::SILOptions&) + 38
33 swift                    0x0000000102be1e28 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 7576
34 swift                    0x0000000102bdf160 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3024
35 swift                    0x0000000102b86fd9 main + 729
36 libdyld.dylib            0x00007fff7d8e33d5 start + 1
37 libdyld.dylib            0x000000000000000f start + 2188495931
fish: './swiftc -emit-silgen /Users/ha…' terminated by signal SIGABRT (Abort)

Really we probably shouldn't be letting this code past Sema, as we currently reject the non-optional form (albeit with a suboptimal diagnostic):

func foo(_ x: @autoclosure () -> UnsafePointer<Int>) {}

var i = 0
foo(&i) // error: 'inout Int' is not convertible to 'UnsafePointer<Int>'
@belkadan
Copy link
Contributor

I guess we shouldn't allow inout-to-pointer (or any other lifetime-based conversions) in autoclosure contexts. cc @xedin

@xedin
Copy link
Member

xedin commented Jul 25, 2019

@swift-ci create

Not allowing such conversions in @autoclosure make sense to me!

@xedin
Copy link
Member

xedin commented Jul 29, 2019

/cc @hborla Might be a good StarterBug for expression type-checker

@LucianoPAlmeida
Copy link
Collaborator

Is anyone working on this already? If not, since is a starter bug I'd like to give it a shot

@xedin
Copy link
Member

xedin commented Aug 2, 2019

@hborla has started working on this. Sorry we didn't update the assignee...

@LucianoPAlmeida
Copy link
Collaborator

Sure, thank's for the answer @xedin 👍

@hborla
Copy link
Member

hborla commented Aug 2, 2019

@LucianoPAlmeida I haven't started working on the crash part of this yet. I'm finishing up improving the diagnostic for the non-optional case, PR is up now https://github.com/apple/swift/pull/26470

Do you still want to give this a shot?

@xedin
Copy link
Member

xedin commented Aug 2, 2019

@hborla Since PR allows conversion between type and pointer, it might solve the crash as well, because it should be able to attempt implicit conversion from `UnsafePointer<Int>` to `UnsafePointer<Int>`?

@hamishknight
Copy link
Collaborator Author

Unfortunately the problem is that within matchTypes we're only looking at the locator's last path element to determine if it's an autoclosure argument application. When we perform a conversion like value-to-optional we add another path element to the locator, so matchTypes no longer thinks we're dealing with an autoclosure. We'll need to tweak matchTypes to look through various path elements before checking for autoclosure.

@xedin
Copy link
Member

xedin commented Aug 3, 2019

We already do check wether left-hand side of the type is autoclosure result, so we just need to tweak that check a bit to account for presence of optional. Sorry I got confused about what we are trying to do here.

@LucianoPAlmeida
Copy link
Collaborator

@hborla Thank's, but I've just started with SR-10597 so feel free to continue with this one 🙂

@hborla
Copy link
Member

hborla commented Aug 21, 2019

#26541

@xedin
Copy link
Member

xedin commented Aug 21, 2019

Please verify and close.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added the crash Bug: A crash, i.e., an abnormal termination of software label Dec 12, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

6 participants