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-11559] Iterating over ClosedRange<Character> crashes the compiler #53964

Closed
ole opened this issue Oct 2, 2019 · 6 comments
Closed

[SR-11559] Iterating over ClosedRange<Character> crashes the compiler #53964

ole opened this issue Oct 2, 2019 · 6 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 type checker Area → compiler: Semantic analysis

Comments

@ole
Copy link
Contributor

ole commented Oct 2, 2019

Previous ID SR-11559
Radar rdar://problem/55987389
Original Reporter @ole
Type Bug
Status Closed
Resolution Done
Environment

I'm using the Swift that's part of Xcode 11.1 GM seed (11A1027) on macOS 10.15 Developer Beta 10.

$ swift --version
Apple Swift version 5.1 (swiftlang-1100.0.270.13 clang-1100.0.33.7)
Target: x86_64-apple-darwin19.0.0

I haven't tested this on the current master branch.

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

md5: 9ed2a900fe6fda0b29edeb0e0e2f4d73

Issue Description:

This (invalid) code crashes the compiler:

// main.swift
let a: Character = "a"
let z: Character = "z"
let charRange = a...z
for c in charRange {
    print(c)
}
$ swift main.swift
Stack dump:
0.  Program arguments: /Applications/Xcode-11.1.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -interpret main.swift -enable-objc-interop -sdk /Applications/Xcode-11.1.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -color-diagnostics -module-name main
1.  While type-checking statement at [main.swift:4:1 - line:6:1] RangeText="for c in charRange {
    print(c)
"
2.  While type-checking statement at [main.swift:4:1 - line:6:1] RangeText="for c in charRange {
    print(c)
"
3.  While type-checking expression at [main.swift:4:10 - line:4:10] RangeText=""
0  swift                    0x0000000105503eb3 PrintStackTraceSignalHandler(void*) + 51
1  swift                    0x0000000105503686 SignalHandler(int) + 358
2  libsystem_platform.dylib 0x00007fff69f4fb1d _sigtramp + 29
3  libsystem_platform.dylib 0x00007ffeeeb3c560 _sigtramp + 2227096160
4  swift                    0x0000000101bad136 swift::constraints::MissingConformance::diagnose(swift::Expr*, bool) const + 406
5  swift                    0x0000000101ae3994 swift::constraints::ConstraintSystem::applySolutionFixes(swift::Expr*, swift::constraints::Solution const&)::DiagnosticWalker::diagnose(swift::Expr*) + 260
6  swift                    0x0000000101ae3747 swift::constraints::ConstraintSystem::applySolutionFixes(swift::Expr*, swift::constraints::Solution const&)::DiagnosticWalker::walkToExprPost(swift::Expr*) + 23
7  swift                    0x0000000101c9191f swift::TypeChecker::typeCheckExpressionImpl(swift::Expr*&, swift::DeclContext*, swift::TypeLoc, swift::ContextualTypePurpose, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>, swift::ExprTypeCheckListener&, swift::constraints::ConstraintSystem*) + 2383
8  swift                    0x0000000101d3fbf1 swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 8241
9  swift                    0x0000000101d43e21 bool (anonymous namespace)::StmtChecker::typeCheckStmt<swift::Stmt>(swift::Stmt*&) + 129
10 swift                    0x0000000101d438b3 swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 23795
11 swift                    0x0000000101d3db61 bool (anonymous namespace)::StmtChecker::typeCheckStmt<swift::BraceStmt>(swift::BraceStmt*&) + 129
12 swift                    0x0000000101d46d67 swift::TypeChecker::typeCheckTopLevelCodeDecl(swift::TopLevelCodeDecl*) + 263
13 swift                    0x0000000101d62118 swift::performTypeChecking(swift::SourceFile&, swift::TopLevelContext&, swift::OptionSet<swift::TypeCheckingFlags, unsigned int>, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int) + 1656
14 swift                    0x000000010145ce57 swift::CompilerInstance::performSemaUpTo(swift::SourceFile::ASTStage_t) + 4679
15 swift                    0x000000010115298f performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 815
16 swift                    0x000000010114ee54 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 6820
17 swift                    0x00000001010dc3c3 main + 1219
18 libdyld.dylib            0x00007fff69d4e405 start + 1
fish: 'swift main.swift' terminated by signal SIGSEGV (Address boundary error)

I would have expected a compiler error because charRange is not a Sequence so the for loop should not compile. Interestingly, that's exactly what I get when I remove the charRange variable and create the range directly in the for loop. This behaves correctly:

let a: Character = "a"
let z: Character = "z"
for c in a...z {
    print(c)
}
main.swift:3:11: error: operator function '...' requires that 'Character.Stride' conform to 'SignedInteger'
for c in a...z {
          ^
Swift.ClosedRange<τ_0_0>:1:11: note: requirement from conditional conformance of 'ClosedRange<Character>' to 'Sequence'
extension ClosedRange : Sequence where Bound : Strideable, Bound.Stride : SignedInteger {
          ^
main.swift:3:11: error: operator function '...' requires that 'Character' conform to 'Strideable'
for c in a...z {
          ^
Swift.ClosedRange<τ_0_0>:1:11: note: requirement from conditional conformance of 'ClosedRange<Character>' to 'Sequence'
extension ClosedRange : Sequence where Bound : Strideable, Bound.Stride : SignedInteger {
@belkadan
Copy link
Contributor

belkadan commented Oct 2, 2019

I get basically the same failure on master. Thanks for reporting!

@swift-ci create

@LucianoPAlmeida
Copy link
Collaborator

@belkadan Is someone working on this already? If not can I give it a try if it is still not fixed? 🙂

@belkadan
Copy link
Contributor

I don't think anyone's made particular progress. I have no idea what the fix is going to be but @hborla and @xedin should be able to help some!*

* note that I didn't check with them to see if they're currently busy with other things, so if they don't respond right away it's my fault

@hborla
Copy link
Member

hborla commented Oct 25, 2019

@xedin recently added a tailored diagnostic for for-in sequence failures, so this might be fixed, as it should no longer be diagnosed through the MissingConformance constraint fix, but rather the ContextualMismatch fix. I don't have those changes locally though, so I'll let Pavel answer that!

@xedin
Copy link
Member

xedin commented Oct 25, 2019

This problem has indeed been addressed by the changes @hborla mentioned and would produce a diagnostic:

error: protocol 'Sequence' requires that 'Character' conform to 'Strideable'
for c in charRange {
         ^
swift/stdlib/public/core/ClosedRange.swift:123:1: note: requirement from conditional conformance of 'ClosedRange<Character>' to 'Sequence'
extension ClosedRange: Sequence
^

Please use @ole please use the next available nightly build to verify and close.

@ole
Copy link
Contributor Author

ole commented Oct 26, 2019

@xedin I can confirm that this is fixed in DEVELOPMENT-SNAPSHOT-2019-10-24-a. Thanks a lot everyone!

@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

6 participants