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
The text was updated successfully, but these errors were encountered:
<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) { }
^
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?
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
md5: 51eb6d12c1409f11f2a249e9cf9539df
relates to:
let
sIssue Description:
I'm getting a compiler crash with code that looks like this:
The issue appears to be with the if statement:
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):
The text was updated successfully, but these errors were encountered: