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-4082] Compiler Crash on multi-statement if with shadowed variable #46665

Closed
saagarjha opened this issue Feb 27, 2017 · 4 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

Comments

@saagarjha
Copy link
Contributor

Previous ID SR-4082
Radar None
Original Reporter @saagarjha
Type Bug
Status Resolved
Resolution Done
Environment

macOS Sierra 10.12.4 Beta (16E163f)
Xcode Version 8.3 beta 2 (8W120l)
Toolchain: Xcode 8.3, Swift Development Snapshot 2/24/17

Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug, 3.1Regression, CompilerCrash
Assignee @gregomni
Priority Medium

md5: 51eb6d12c1409f11f2a249e9cf9539df

relates to:

  • SR-4387 Compiler Segmentation Fault 11 on wrongly ordered lets

Issue Description:

I'm getting a compiler crash with code that looks like this:

import Foundation

struct Percent {
    var percent: String
    
    init(percent: String) {
        self.percent = percent
    }
    
    var percentValue: Double? {
        get {
            if percent.hasSuffix("%"),
                let percent = double(for: percent) {
                return percent / 100
            }
            return nil
        }
    }
    
    func double(for percent: String) -> Double? {
        return Double(percent.substring(to: percent.index(before: percent.endIndex)))
    }
}

print(Percent(percent: "42%").percentValue ?? "This isn't a percent.")

The issue appears to be with the if statement:

if percent.hasSuffix("%"),
    let percent = double(for: percent) {
    return percent / 100
}

If I remove the first statement, or change the if-let to use some other variable name instead of shadowing, the issue goes away. Here's a log with the latest Development Snapshot (2/24/17):

Assertion failed: ((HadError || !M.is<SourceFile*>() || M.get<SourceFile*>()->ASTStage < SourceFile::TypeChecked) && "UnresolvedDot" "in wrong phase"), function walkToExprPre, file /Users/buildnode/jenkins/workspace/oss-swift-package-osx/swift/include/swift/AST/ExprNodes.def, line 87.
0  swift                    0x000000010baee0b8 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 40
1  swift                    0x000000010baed2d6 llvm::sys::RunSignalHandlers() + 86
2  swift                    0x000000010baee709 SignalHandler(int) + 361
3  libsystem_platform.dylib 0x00007fffbe67cb3a _sigtramp + 26
4  swift                    0x000000010996f044 swift::TypeBase::getDesugaredType() + 20
5  libsystem_c.dylib        0x00007fffbe501420 abort + 129
6  libsystem_c.dylib        0x00007fffbe4c8893 basename_r + 0
7  swift                    0x00000001098b8327 (anonymous namespace)::Verifier::walkToExprPre(swift::Expr*) + 263
8  swift                    0x00000001098ca804 (anonymous namespace)::Traversal::visitApplyExpr(swift::ApplyExpr*) + 36
9  swift                    0x00000001098c6b4b (anonymous namespace)::Traversal::doIt(llvm::MutableArrayRef<swift::StmtConditionElement> const&) + 363
10 swift                    0x00000001098caf32 swift::ASTVisitor<(anonymous namespace)::Traversal, swift::Expr*, swift::Stmt*, bool, swift::Pattern*, bool, void>::visit(swift::Stmt*) + 1074
11 swift                    0x00000001098cacbd swift::ASTVisitor<(anonymous namespace)::Traversal, swift::Expr*, swift::Stmt*, bool, swift::Pattern*, bool, void>::visit(swift::Stmt*) + 445
12 swift                    0x00000001098cd53a (anonymous namespace)::Traversal::visitAbstractFunctionDecl(swift::AbstractFunctionDecl*) + 1434
13 swift                    0x00000001098c6dfe (anonymous namespace)::Traversal::doIt(swift::Decl*) + 366
14 swift                    0x00000001098ccf24 (anonymous namespace)::Traversal::visitNominalTypeDecl(swift::NominalTypeDecl*) + 1076
15 swift                    0x00000001098c6ded (anonymous namespace)::Traversal::doIt(swift::Decl*) + 349
16 swift                    0x00000001098c6c7b swift::Decl::walk(swift::ASTWalker&) + 27
17 swift                    0x000000010994499a swift::SourceFile::walk(swift::ASTWalker&) + 170
18 swift                    0x00000001098b7cbb swift::verify(swift::SourceFile&) + 59
19 swift                    0x000000010983fcb3 swift::performTypeChecking(swift::SourceFile&, swift::TopLevelContext&, swift::OptionSet<swift::TypeCheckingFlags, unsigned int>, unsigned int, unsigned int) + 2067
20 swift                    0x00000001094433d9 swift::CompilerInstance::performSema() + 3897
21 swift                    0x00000001089fd38c swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 4380
22 swift                    0x00000001089bbd70 main + 3312
23 libdyld.dylib            0x00007fffbe46d235 start + 1

Stack dump:
0.  While walking into decl 'Percent' at /var/folders/x3/wncmj8p52t732rrxyz6h424m0000gn/T/./lldb/56770/playground1.swift:3:1
1.  While walking into body of getter for percentValue at /var/folders/x3/wncmj8p52t732rrxyz6h424m0000gn/T/./lldb/56770/playground1.swift:10:6
@swift-ci
Copy link
Collaborator

Comment by Pawel Szot (JIRA)

Minimal code:

let x = 5
if x > 0, let x = Optional(1) { }

@belkadan
Copy link
Contributor

In 3.0 this gave an arguably bogus error:

<stdin>:2:4: error: use of local variable 'x' before its declaration
if x > 0, let x = Optional(1) { }
   ^
<stdin>:2:15: note: 'x' declared here
if x > 0, let x = Optional(1) { }
              ^

@saagarjha
Copy link
Contributor Author

pszot (JIRA User) I was too tired last night dealing with iTunes Connect to pare it down. Thanks for the minimal reproduction.

@gregomni
Copy link
Collaborator

gregomni commented Oct 14, 2017

Fixed in #12438. The result now is:

4082.swift:2:15: warning: immutable value 'x' was never used; consider replacing with '_' or removing it
if x > 0, let x = Optional(1) { }
              ^
              _

I.e. the x in "x > 0" is the one whose value is 5, and then the new x is unused. Should there be some additional shadowing warning of some kind here, do you think?

@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
Projects
None yet
Development

No branches or pull requests

5 participants