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-11893] Compiler crash on unwrapped IUO key path member used in dynamic lookup #54310

Closed
hamishknight opened this issue Dec 4, 2019 · 1 comment
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 key paths Feature: key paths (both native and Objective-C) type checker Area → compiler: Semantic analysis

Comments

@hamishknight
Copy link
Collaborator

Previous ID SR-11893
Radar None
Original Reporter @hamishknight
Type Bug
Status Closed
Resolution Done
Environment

Swift version 5.1.2-dev (Swift b7537cf)
Target: x86_64-apple-darwin18.5.0

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

md5: 57572def123cc8edaca1b0ba24ddcd72

Issue Description:

The following hits an assertion in SILGen:

struct R {
  var i: Int!
}

@dynamicMemberLookup
struct S {
  subscript(dynamicMember kp: KeyPath<R, Int>) -> Void { () }
}

func foo(_ x: S) {
  x.i
}
Stack dump:

SIL verification failed: keypath value type should match value type of keypath pattern: leafTy == pattern->getValueType().subst(patternSubs)->getCanonicalType()
Verifying instruction:
->   %2 = keypath $KeyPath<R, Int>, (root $R; stored_property #R.i : $Optional<Int>) // users: %5, %4
     %4 = apply %3(%2, %0) : $@convention(method) (@guaranteed KeyPath<R, Int>, S) -> ()
     destroy_value %2 : $KeyPath<R, Int>          // id: %5
In function:
// foo(_:)
sil hidden [ossa] @$s4main3fooyyAA1SVF : $@convention(thin) (S) -> () {
// %0                                             // users: %4, %1
bb0(%0 : $S):
  debug_value %0 : $S, let, name "x", argno 1     // id: %1
  %2 = keypath $KeyPath<R, Int>, (root $R; stored_property #R.i : $Optional<Int>) // users: %5, %4
  // function_ref S.subscript.getter
  %3 = function_ref @$s4main1SV13dynamicMemberys7KeyPathCyAA1RVSiG_tcig : $@convention(method) (@guaranteed KeyPath<R, Int>, S) -> () // user: %4
  %4 = apply %3(%2, %0) : $@convention(method) (@guaranteed KeyPath<R, Int>, S) -> ()
  destroy_value %2 : $KeyPath<R, Int>             // id: %5
  %6 = tuple ()                                   // user: %7
  return %6 : $()                                 // id: %7
} // end sil function '$s4main3fooyyAA1SVF'

Stack dump:
0.  Program arguments: /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2019-11-29-a.xctoolchain/usr/bin/swift -frontend -emit-sil /Users/hamishknight/Desktop/Stochastic Projects/newnew/newnew/main.swift
1.  Apple Swift version 5.1.1-dev (Swift d2e1f09169)
2.  While emitting SIL for 'foo(_:)' (at /Users/hamishknight/Desktop/Stochastic Projects/newnew/newnew/main.swift:750:1)
3.  While silgen emitFunction SIL function "@$s4main3fooyyAA1SVF".
 for 'foo(_:)' (at /Users/hamishknight/Desktop/Stochastic Projects/newnew/newnew/main.swift:750:1)
4.  While verifying SIL function "@$s4main3fooyyAA1SVF".
 for 'foo(_:)' (at /Users/hamishknight/Desktop/Stochastic Projects/newnew/newnew/main.swift:750:1)
0  swift                    0x0000000105f6f6f5 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 37
1  swift                    0x0000000105f6e905 llvm::sys::RunSignalHandlers() + 85
2  swift                    0x0000000105f6fce6 SignalHandler(int) + 278
3  libsystem_platform.dylib 0x00007fff68536b5d _sigtramp + 29
4  swift                    0x0000000107e20000 (anonymous namespace)::DarwinX86AsmBackend::getCompactUnwindRegNum(unsigned int) const::CU64BitRegs + 236198
5  libsystem_c.dylib        0x00007fff683f66a6 abort + 127
6  swift                    0x0000000102b9df18 (anonymous namespace)::SILVerifier::_require(bool, llvm::Twine const&, std::__1::function<void ()> const&) + 616
7  swift                    0x0000000102bad4d0 swift::SILInstructionVisitor<(anonymous namespace)::SILVerifier, void>::visit(swift::SILInstruction*) + 51040
8  swift                    0x0000000102b9fafc (anonymous namespace)::SILVerifier::visitSILBasicBlock(swift::SILBasicBlock*) + 1484
9  swift                    0x0000000102b9a1d7 swift::SILFunction::verify(bool) const + 7703
10 swift                    0x000000010260b455 swift::Lowering::SILGenModule::postEmitFunction(swift::SILDeclRef, swift::SILFunction*) + 213
11 swift                    0x0000000102611bd0 swift::Lowering::SILGenModule::emitFunction(swift::FuncDecl*)::$_0::operator()(swift::SILFunction*) const + 256
12 swift                    0x000000010260a281 swift::Lowering::SILGenModule::emitFunction(swift::FuncDecl*) + 609
13 swift                    0x000000010260f2d3 swift::Lowering::SILGenModule::emitSourceFile(swift::SourceFile*) + 915
14 swift                    0x0000000102610229 swift::SILModule::constructSIL(swift::ModuleDecl*, swift::Lowering::TypeConverter&, swift::SILOptions&, swift::FileUnit*) + 313
15 swift                    0x00000001026105e1 swift::performSILGeneration(swift::ModuleDecl*, swift::Lowering::TypeConverter&, swift::SILOptions&) + 17
16 swift                    0x0000000102312f49 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 8713
17 swift                    0x000000010230fdc6 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3046
18 swift                    0x00000001022aa8ae main + 734
19 libdyld.dylib            0x00007fff683513d5 start + 1
20 libdyld.dylib            0x0000000000000004 start + 2546658352
fish: '/Library/Developer/Toolchains/s…' terminated by signal SIGABRT (Abort)

And in a no-asserts build it miscompiles:

struct R {
  var i: Int!
}

@dynamicMemberLookup
struct S {
  subscript(dynamicMember kp: KeyPath<R, Int>) -> Int {
    return R()[keyPath: kp]
  }
}

func foo(_ x: S) {
  print(x.i) // prints: 0
}

foo(S())
@hamishknight
Copy link
Collaborator Author

Fixed by #28562

@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 key paths Feature: key paths (both native and Objective-C) type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

2 participants