swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
66.76k stars 10.29k forks source link

Using Self with variadic generic type caused compiler crash #74960

Open rayx opened 3 days ago

rayx commented 3 days ago

Description

The following code crashes compiler:

protocol Entity {
    var name: String { get }
}

struct DataStore<each T: Entity> {
    var entities: (repeat [each T])

    func combine(another: Self) -> Self {
        Self(entities: repeat (each entities + each another.entities))
    }
}

Reproduction

Putting the above code in a swift package and run swift build.

Stack dump

% swift build
warning: could not determine XCTest paths: terminated(1): /usr/bin/xcrun --sdk macosx --show-sdk-platform-path output:
    xcrun: error: unable to lookup item 'PlatformPath' from command line tools installation
    xcrun: error: unable to lookup item 'PlatformPath' in SDK '/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk'

Building for debugging...
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0.  Program arguments: /Library/Developer/CommandLineTools/usr/bin/swift-frontend -frontend -c -primary-file /Users/xh01983184/sandbox/variadictTest2/Sources/variadictTest2/variadictTest2.swift -emit-module-path /Users/xh01983184/sandbox/variadictTest2/.build/arm64-apple-macosx/debug/variadictTest2.build/variadictTest2~partial.swiftmodule -emit-module-doc-path /Users/xh01983184/sandbox/variadictTest2/.build/arm64-apple-macosx/debug/variadictTest2.build/variadictTest2~partial.swiftdoc -emit-module-source-info-path /Users/xh01983184/sandbox/variadictTest2/.build/arm64-apple-macosx/debug/variadictTest2.build/variadictTest2~partial.swiftsourceinfo -emit-dependencies-path /Users/xh01983184/sandbox/variadictTest2/.build/arm64-apple-macosx/debug/variadictTest2.build/variadictTest2.d -emit-reference-dependencies-path /Users/xh01983184/sandbox/variadictTest2/.build/arm64-apple-macosx/debug/variadictTest2.build/variadictTest2.swiftdeps -target arm64-apple-macosx14.0 -Xllvm -aarch64-use-tbi -enable-objc-interop -sdk /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -I /Users/xh01983184/sandbox/variadictTest2/.build/arm64-apple-macosx/debug -color-diagnostics -enable-testing -g -module-cache-path /Users/xh01983184/sandbox/variadictTest2/.build/arm64-apple-macosx/debug/ModuleCache -package-name variadicttest2 -swift-version 5 -Onone -D SWIFT_PACKAGE -D DEBUG -enable-anonymous-context-mangled-names -external-plugin-path /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib/swift/host/plugins#/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/bin/swift-plugin-server -external-plugin-path /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/local/lib/swift/host/plugins#/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/bin/swift-plugin-server -external-plugin-path /Library/Developer/Developer/usr/lib/swift/host/plugins#/Library/Developer/Developer/usr/bin/swift-plugin-server -external-plugin-path /Library/Developer/Developer/usr/local/lib/swift/host/plugins#/Library/Developer/Developer/usr/bin/swift-plugin-server -plugin-path /Library/Developer/CommandLineTools/usr/lib/swift/host/plugins -plugin-path /Library/Developer/CommandLineTools/usr/local/lib/swift/host/plugins -Xcc -isysroot -Xcc /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -Xcc -fPIC -Xcc -g -target-sdk-version 14.4 -parse-as-library -module-name variadictTest2 -o /Users/xh01983184/sandbox/variadictTest2/.build/arm64-apple-macosx/debug/variadictTest2.build/variadictTest2.swift.o -index-store-path /Users/xh01983184/sandbox/variadictTest2/.build/arm64-apple-macosx/debug/index/store -index-system-modules
1.  Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
2.  Compiling with the current language version
3.  While evaluating request TypeCheckSourceFileRequest(source_file "/Users/xh01983184/sandbox/variadictTest2/Sources/variadictTest2/variadictTest2.swift")
4.  While evaluating request TypeCheckFunctionBodyRequest(variadictTest2.(file).DataStore.combine(other:)@/Users/xh01983184/sandbox/variadictTest2/Sources/variadictTest2/variadictTest2.swift:11:7)
5.  While type-checking statement at [/Users/xh01983184/sandbox/variadictTest2/Sources/variadictTest2/variadictTest2.swift:11:36 - line:13:2] RangeText="{
        Self(entities: repeat (each entities + each other.entities))
    "
6.  While type-checking statement at [/Users/xh01983184/sandbox/variadictTest2/Sources/variadictTest2/variadictTest2.swift:12:3 - line:12:62] RangeText="Self(entities: repeat (each entities + each other.entities)"
7.  While type-checking expression at [/Users/xh01983184/sandbox/variadictTest2/Sources/variadictTest2/variadictTest2.swift:12:3 - line:12:62] RangeText="Self(entities: repeat (each entities + each other.entities)"
8.  While type-checking-target starting at /Users/xh01983184/sandbox/variadictTest2/Sources/variadictTest2/variadictTest2.swift:12:3
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           0x0000000107ad7f3c llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000107ad70f8 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000107ad8544 SignalHandler(int) + 360
3  libsystem_platform.dylib 0x0000000184b31a24 _sigtramp + 56
4  swift-frontend           0x0000000103436354 (anonymous namespace)::ExprRewriter::coerceToType(swift::Expr*, swift::Type, swift::constraints::ConstraintLocatorBuilder) + 5284
5  swift-frontend           0x000000010344aedc (anonymous namespace)::ExprRewriter::coerceCallArguments(swift::ArgumentList*, swift::AnyFunctionType*, swift::ConcreteDeclRef, swift::ApplyExpr*, swift::constraints::ConstraintLocatorBuilder, llvm::ArrayRef<swift::AppliedPropertyWrapper>) + 6444
6  swift-frontend           0x00000001034550cc (anonymous namespace)::ExprRewriter::finishApply(swift::ApplyExpr*, swift::Type, swift::constraints::ConstraintLocatorBuilder, swift::constraints::ConstraintLocatorBuilder) + 6100
7  swift-frontend           0x0000000103455664 (anonymous namespace)::ExprRewriter::finishApply(swift::ApplyExpr*, swift::Type, swift::constraints::ConstraintLocatorBuilder, swift::constraints::ConstraintLocatorBuilder) + 7532
8  swift-frontend           0x000000010345bd34 (anonymous namespace)::ExprRewriter::visitApplyExpr(swift::ApplyExpr*) + 508
9  swift-frontend           0x000000010343c6a8 (anonymous namespace)::ExprRewriter::walkToExprPost(swift::Expr*) + 120
10 swift-frontend           0x0000000103438830 (anonymous namespace)::ExprWalker::walkToExprPost(swift::Expr*) + 24
11 swift-frontend           0x0000000103433564 (anonymous namespace)::ExprWalker::rewriteTarget(swift::constraints::SyntacticElementTarget) + 2720
12 swift-frontend           0x00000001034325d0 swift::constraints::ConstraintSystem::applySolution(swift::constraints::Solution&, swift::constraints::SyntacticElementTarget) + 6984
13 swift-frontend           0x00000001036f7b94 swift::TypeChecker::typeCheckTarget(swift::constraints::SyntacticElementTarget&, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 660
14 swift-frontend           0x00000001036f77dc swift::TypeChecker::typeCheckExpression(swift::constraints::SyntacticElementTarget&, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 436
15 swift-frontend           0x00000001036f75d4 swift::TypeChecker::typeCheckExpression(swift::Expr*&, swift::DeclContext*, swift::constraints::ContextualTypeInfo, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 92
16 swift-frontend           0x000000010380bcdc swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 7136
17 swift-frontend           0x0000000103809fc4 bool (anonymous namespace)::StmtChecker::typeCheckStmt<swift::Stmt>(swift::Stmt*&) + 316
18 swift-frontend           0x0000000103805008 (anonymous namespace)::StmtChecker::typeCheckASTNode(swift::ASTNode&) + 104
19 swift-frontend           0x000000010380a224 swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 296
20 swift-frontend           0x0000000103807a6c bool (anonymous namespace)::StmtChecker::typeCheckStmt<swift::BraceStmt>(swift::BraceStmt*&) + 316
21 swift-frontend           0x00000001038067dc swift::TypeCheckFunctionBodyRequest::evaluate(swift::Evaluator&, swift::AbstractFunctionDecl*) const + 732
22 swift-frontend           0x0000000103be7f68 swift::TypeCheckFunctionBodyRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckFunctionBodyRequest>(swift::Evaluator&, swift::TypeCheckFunctionBodyRequest, swift::TypeCheckFunctionBodyRequest::OutputType) + 884
23 swift-frontend           0x0000000103854520 swift::TypeCheckSourceFileRequest::evaluate(swift::Evaluator&, swift::SourceFile*) const + 608
24 swift-frontend           0x000000010385a988 llvm::Expected<swift::TypeCheckSourceFileRequest::OutputType> swift::Evaluator::getResultUncached<swift::TypeCheckSourceFileRequest>(swift::TypeCheckSourceFileRequest const&) + 664
25 swift-frontend           0x0000000103854240 swift::TypeCheckSourceFileRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckSourceFileRequest>(swift::Evaluator&, swift::TypeCheckSourceFileRequest, swift::TypeCheckSourceFileRequest::OutputType) + 228
26 swift-frontend           0x00000001025e4854 swift::CompilerInstance::performSema() + 196
27 swift-frontend           0x00000001023d9634 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1352
28 swift-frontend           0x00000001023d76d0 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 4968
29 swift-frontend           0x0000000102366e8c swift::mainEntry(int, char const**) + 2612
30 dyld                     0x00000001847810e0 start + 2360

Expected behavior

I think the code should compile.

Environment

% swift --version Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4) Target: arm64-apple-darwin23.2.0

Additional information

No response

AnthonyLatsis commented 3 days ago

Reduced:

struct S<T> {}

func foo<each T>(_: (repeat S<each T>)) {}

func test<each T>(_ array: repeat S<each T>) {
  foo(repeat each array) // crash
  foo((repeat each array)) // ok
}