Skip to content

Type checker crashes when type-checking 0 literal #63603

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

Open
MaxDesiatov opened this issue Feb 12, 2023 · 1 comment
Open

Type checker crashes when type-checking 0 literal #63603

MaxDesiatov opened this issue Feb 12, 2023 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself crash Bug: A crash, i.e., an abnormal termination of software literals Feature → expressions: Literals such as an integer or string literal numerics Area → standard library: Numeric API type checker Area → compiler: Semantic analysis

Comments

@MaxDesiatov
Copy link
Contributor

Steps to reproduce:

  1. Create a file Repro.swift with this Swift code:
precedencegroup AssignmentPrecedence {
  assignment: true
  associativity: right
}

protocol _ExpressibleByBuiltinIntegerLiteral {
  init(_builtinIntegerLiteral value: Builtin.IntLiteral)
}

protocol ExpressibleByIntegerLiteral {
  associatedtype IntegerLiteralType = _ExpressibleByBuiltinIntegerLiteral
  init(integerLiteral value: IntegerLiteralType)
}

#if arch(arm64) || arch(powerpc64)
  typealias _MaxBuiltinFloatType = Builtin.FPIEEE128
#elseif !(os(Windows) || os(Android)) && (arch(i386) || arch(x86_64))
  typealias _MaxBuiltinFloatType = Builtin.FPIEEE80
#else
  typealias _MaxBuiltinFloatType = Builtin.FPIEEE64
#endif

protocol _ExpressibleByBuiltinFloatLiteral {
  init(_builtinFloatLiteral value: _MaxBuiltinFloatType)
}

protocol ExpressibleByFloatLiteral {
  associatedtype FloatLiteralType: _ExpressibleByBuiltinFloatLiteral
  init(floatLiteral value: FloatLiteralType)
}

protocol AdditiveArithmetic {
  static var zero: Self { get }
}

extension AdditiveArithmetic where Self: ExpressibleByIntegerLiteral {
  static var zero: Self {
    0
  }
}

Try to compile it with swiftc -emit-library -module-name Swift -module-link-name swiftCore -parse-stdlib Repro.swift

Actual result

error: compile command failed due to signal 11 (use -v to see invocation)
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project and the crash backtrace.
Stack dump:
0.	Program arguments: /Applications/Xcode-14.2.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file Repro.swift -disable-objc-attr-requires-foundation-module -target arm64-apple-macosx13.0 -Xllvm -aarch64-use-tbi -enable-objc-interop -stack-check -sdk /Applications/Xcode-14.2.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.1.sdk -color-diagnostics -module-link-name swiftCore -parse-stdlib -new-driver-path /Applications/Xcode-14.2.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-driver -empty-abi-descriptor -resource-dir /Applications/Xcode-14.2.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -module-name Swift -disable-clang-spi -target-sdk-version 13.1 -parse-as-library -o /var/folders/0k/y5p_js3n0px8m267zq95dkd40000gn/T/TemporaryDirectory.klVIoC/Repro-1.o
1.	Apple Swift version 5.7.2 (swiftlang-5.7.2.135.5 clang-1400.0.29.51)
2.	Compiling with the current language version
3.	While evaluating request TypeCheckSourceFileRequest(source_file "Repro.swift")
4.	While evaluating request TypeCheckFunctionBodyRequest(Swift.(file).AdditiveArithmetic [email protected]:41:25)
5.	While type-checking statement at [Repro.swift:41:25 - line:43:3] RangeText="{
    0
  "
6.	While type-checking statement at [Repro.swift:42:5 - line:42:5] RangeText=""
7.	While type-checking expression at [Repro.swift:42:5 - line:42:5] RangeText=""
8.	While type-checking-target starting at Repro.swift:42:5
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  swift-frontend           0x000000010714b5b0 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x000000010714a5b4 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x000000010714bc34 SignalHandler(int) + 344
3  libsystem_platform.dylib 0x000000019c1182a4 _sigtramp + 56
4  swift-frontend           0x0000000103572ee8 swift::TypeChecker::conformsToProtocol(swift::Type, swift::ProtocolDecl*, swift::ModuleDecl*, bool) + 40
5  swift-frontend           0x0000000103572ee8 swift::TypeChecker::conformsToProtocol(swift::Type, swift::ProtocolDecl*, swift::ModuleDecl*, bool) + 40
6  swift-frontend           0x0000000103284140 (anonymous namespace)::ExprRewriter::convertLiteralInPlace(swift::LiteralExpr*, swift::Type, swift::ProtocolDecl*, swift::Identifier, swift::DeclName, swift::ProtocolDecl*, swift::DeclName, swift::Diag<>, swift::Diag<>) + 464
7  swift-frontend           0x0000000103284190 (anonymous namespace)::ExprRewriter::convertLiteralInPlace(swift::LiteralExpr*, swift::Type, swift::ProtocolDecl*, swift::Identifier, swift::DeclName, swift::ProtocolDecl*, swift::DeclName, swift::Diag<>, swift::Diag<>) + 544
8  swift-frontend           0x0000000103284640 (anonymous namespace)::ExprRewriter::handleIntegerLiteralExpr(swift::LiteralExpr*) + 1028
9  swift-frontend           0x0000000103274d54 (anonymous namespace)::ExprWalker::walkToExprPost(swift::Expr*) + 24
10 swift-frontend           0x000000010326aa94 (anonymous namespace)::ExprWalker::rewriteTarget(swift::constraints::SolutionApplicationTarget) + 408
11 swift-frontend           0x000000010326a5e8 swift::constraints::ConstraintSystem::applySolution(swift::constraints::Solution&, swift::constraints::SolutionApplicationTarget) + 5852
12 swift-frontend           0x00000001034d2100 swift::TypeChecker::typeCheckTarget(swift::constraints::SolutionApplicationTarget&, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 688
13 swift-frontend           0x00000001034d1de0 swift::TypeChecker::typeCheckExpression(swift::constraints::SolutionApplicationTarget&, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 400
14 swift-frontend           0x00000001035b10fc swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 6116
15 swift-frontend           0x00000001035af88c bool (anonymous namespace)::StmtChecker::typeCheckStmt<swift::Stmt>(swift::Stmt*&) + 300
16 swift-frontend           0x00000001035ac484 (anonymous namespace)::StmtChecker::typeCheckASTNode(swift::ASTNode&) + 80
17 swift-frontend           0x00000001035afa60 swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 328
18 swift-frontend           0x00000001035ae53c bool (anonymous namespace)::StmtChecker::typeCheckStmt<swift::BraceStmt>(swift::BraceStmt*&) + 300
19 swift-frontend           0x00000001035ad388 swift::TypeCheckFunctionBodyRequest::evaluate(swift::Evaluator&, swift::AbstractFunctionDecl*) const + 1276
20 swift-frontend           0x00000001039a6e60 llvm::Expected<swift::TypeCheckFunctionBodyRequest::OutputType> swift::Evaluator::getResultUncached<swift::TypeCheckFunctionBodyRequest>(swift::TypeCheckFunctionBodyRequest const&) + 588
21 swift-frontend           0x0000000103939e60 swift::TypeCheckFunctionBodyRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckFunctionBodyRequest>(swift::Evaluator&, swift::TypeCheckFunctionBodyRequest, swift::TypeCheckFunctionBodyRequest::OutputType) + 116
22 swift-frontend           0x00000001035e9dac swift::TypeCheckSourceFileRequest::evaluate(swift::Evaluator&, swift::SourceFile*) const + 792
23 swift-frontend           0x00000001035ec95c llvm::Expected<swift::TypeCheckSourceFileRequest::OutputType> swift::Evaluator::getResultUncached<swift::TypeCheckSourceFileRequest>(swift::TypeCheckSourceFileRequest const&) + 576
24 swift-frontend           0x00000001035e9a40 swift::performTypeChecking(swift::SourceFile&) + 120
25 swift-frontend           0x000000010280aa60 bool llvm::function_ref<bool (swift::SourceFile&)>::callback_fn<swift::CompilerInstance::performSema()::$_6>(long, swift::SourceFile&) + 16
26 swift-frontend           0x0000000102805158 swift::CompilerInstance::forEachFileToTypeCheck(llvm::function_ref<bool (swift::SourceFile&)>) + 164
27 swift-frontend           0x0000000102805078 swift::CompilerInstance::performSema() + 148
28 swift-frontend           0x0000000102798180 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 4364
29 swift-frontend           0x0000000102739294 swift::mainEntry(int, char const**) + 3940
30 dyld                     0x000000019bdbfe50 start + 2544
@MaxDesiatov MaxDesiatov added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. type checker Area → compiler: Semantic analysis compiler crash numerics Area → standard library: Numeric API literals Feature → expressions: Literals such as an integer or string literal labels Feb 12, 2023
@AnthonyLatsis AnthonyLatsis added compiler The Swift compiler itself crash Bug: A crash, i.e., an abnormal termination of software labels Feb 17, 2023
@hamishknight
Copy link
Contributor

Looks like the issue is that the compiler is expecting the IntegerLiteralType associated type to conform to _ExpressibleByBuiltinIntegerLiteral, e.g it should be written:

  associatedtype IntegerLiteralType: _ExpressibleByBuiltinIntegerLiteral

not:

  associatedtype IntegerLiteralType = _ExpressibleByBuiltinIntegerLiteral

Looks like this may have been a typo

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 itself crash Bug: A crash, i.e., an abnormal termination of software literals Feature → expressions: Literals such as an integer or string literal numerics Area → standard library: Numeric API type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

3 participants