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-5701] Segmentation fault with accidental generic parameter shadowing #48271

Closed
BenchR267 opened this issue Aug 17, 2017 · 8 comments
Closed
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 type checker Area → compiler: Semantic analysis

Comments

@BenchR267
Copy link
Member

Previous ID SR-5701
Radar rdar://problem/33955984
Original Reporter @BenchR267
Type Bug
Status Resolved
Resolution Done

Attachment: Download

Environment

Apple Swift version 4.0 (swiftlang-900.0.59 clang-900.0.34.2)
Target: x86_64-apple-macosx10.9

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

md5: bbe3b12697dd02a5e2f200f05cc319db

relates to:

  • SR-1420 Provide compiler warning when shadowing generic type

Issue Description:

I am not totally sure which part the actual problem is but I was able to reproduce a crash I have in my project in a sample swift file:

struct Box<A: Sequence, B> {
    let a: A
    let b: B

    func instanceMethod<S: Sequence>(_ boxes: S) -> Box<A, B> where S.Element == Box<A, B> {
        return boxes.first(where: {_ in true})!
}

    static func method<A, B, C: Collection>(_ boxes: C) -> Box<A, B> where C.Element == Box<A, B> {
        guard let first = boxes.first else {
            fatalError("Need at least one box!")
        }
        return first.instanceMethod(boxes.dropFirst())
    }
}


let values = (0...1000).map { Box(a: String($0), b: $0) }
let f = Box.method(values) // crashes in segmentation fault

The declaration is not the problem, that works, it only crashes on usage. Here is the dump from this example:

▲ ~/Desktop swift bug.swift
0 swift 0x00000001039e37aa PrintStackTraceSignalHandler(void*) + 42
1 swift 0x00000001039e2be6 SignalHandler(int) + 662
2 libsystem_platform.dylib 0x00007fffa66fdb3a _sigtramp + 26
3 libsystem_platform.dylib 0x00007fbc00000000 _sigtramp + 1502618848
4 swift 0x0000000101543645 swift::NominalTypeDecl::hasFixedLayout() const + 21
5 swift 0x00000001010d7511 (anonymous namespace)::LowerType::visitAnyStructType(swift::CanType, swift::StructDecl*) + 49
6 swift 0x00000001010d62a7 swift::Lowering::TypeConverter::getTypeLowering(swift::Lowering::AbstractionPattern, swift::Type) + 2791
7 swift 0x0000000100c45776 swift::Lowering::SILGenModule::getSILGlobalVariable(swift::VarDecl*, swift::ForDefinition_t) + 1878
8 swift 0x0000000100c0bda0 swift::Lowering::SILGenFunction::emitInitializationForVarDecl(swift::VarDecl*) + 992
9 swift 0x0000000100c0e383 swift::Lowering::SILGenFunction::emitPatternBinding(swift::PatternBindingDecl*, unsigned int) + 83
10 swift 0x0000000100bbfbcf swift::ASTVisitor<swift::Lowering::SILGenModule, void, void, void, void, void, void>::visit(swift::Decl*) + 559
11 swift 0x0000000100bbecab swift::Lowering::SILGenModule::emitSourceFile(swift::SourceFile*, unsigned int) + 1115
12 swift 0x0000000100bc0682 swift::SILModule::constructSIL(swift::ModuleDecl*, swift::SILOptions&, swift::FileUnit*, llvm::Optional<unsigned int>, bool) + 914
13 swift 0x000000010035f4b7 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 13095
14 swift 0x000000010035a924 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 7332
15 swift 0x000000010030ff98 main + 12248
16 libdyld.dylib 0x00007fffa64ee235 start + 1
Stack dump:
0. Program arguments: /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -interpret bug.swift -enable-objc-interop -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -color-diagnostics -module-name bug
[1] 32785 segmentation fault xcrun swift bug.swift

I can also provide you access to the project, it's open source. But I guess the above example is enough.

@belkadan
Copy link
Contributor

Another one of these:

Assertion failed: ((HadError || !M.is<SourceFile*>() || M.get<SourceFile*>()->ASTStage < SourceFile::TypeChecked) && "UnresolvedDot" "in wrong phase"), function walkToExprPre, file /Volumes/Data/swift-public/swift/include/swift/AST/ExprNodes.def, line 87.

1.  While walking into decl declaration 0x7fd6a35a2dd0 at <stdin>:19:1
2.  While walking into initializer for declaration 0x7fd6a35a2fb8 at <stdin>:19:1

The correct way to declare this method is

    static func method<C: Collection>(_ boxes: C) -> Box<A, B> where C.Element == Box<A, B> {
        guard let first = boxes.first else {
            fatalError("Need at least one box!")
        }
        return first.instanceMethod(boxes.dropFirst())
    }

but of course it still shouldn't crash. @huonw, @slavapestov, does this look familiar?

@BenchR267
Copy link
Member Author

Thanks, that works! But why don't I need to specify the generic parameters? Because they are part of the type (Box) signature?

@belkadan
Copy link
Contributor

Yep, that's correct. When you include the A and B in the method's generic parameters, you're shadowing the ones on the type, and then the type checker can't be sure if they're important or not. Diagnosing that is SR-1420.

@BenchR267
Copy link
Member Author

That makes sense, thanks for explanation!

@slavapestov
Copy link
Member

@belkadan I don't know what this problem is off the top of my head but usually this means type substitution produced an ErrorType at some point without emitting any diagnostics.

@slavapestov
Copy link
Member

@swift-ci create

@huonw
Copy link
Mannequin

huonw mannequin commented Aug 23, 2017

The following creduce-reduction seems to crash in the same way, but it doesn't involve shadowing:

struct Box<A: Sequence> {
    static func method<C>(_ : C) -> Box {       }
}
let values = (0...1000).map {        $0 }
             Box.method(values)

@slavapestov
Copy link
Member

I fixed this recently:

x.swift:5:18: error: generic parameter 'A' could not be inferred
             Box.method(values)
                 ^
x.swift:1:8: note: 'A' declared as parameter to type 'Box'
struct Box<A: Sequence> {
       ^

@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 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

4 participants