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

Crash While Type Checking #76521

Open
ed-irl opened this issue Sep 17, 2024 · 0 comments
Open

Crash While Type Checking #76521

ed-irl opened this issue Sep 17, 2024 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software expressions Feature: expressions implicit existential opening Feature → existentials: implicit opening of existentials when passed to parameters of generic type type checker Area → compiler: Semantic analysis

Comments

@ed-irl
Copy link

ed-irl commented Sep 17, 2024

Description

This code compiled under the previous 5.x compiler release (not sure which one - perhaps 5.10?). Xcode upgraded itself today, and now with the swift 6 compiler I get a crash.

Reproduction

import Foundation
import SwiftUI

protocol Workout: AnyObject, Observable, Hashable, Identifiable {
  associatedtype WorkoutExerciseSet: ExerciseSet & Observable

  var workoutId: UUID { get set }
  var name: String { get set }
  var createdAt: Date { get set }
  var updatedAt: Date { get set }
  var sets: [WorkoutExerciseSet] { get set }
}

@Observable class EphemeralExerciseSet: ExerciseSet {
  var order: Int
  var name: String
  var reps: Int
  var resistance: Double
  var resistanceUnit: String
  var markedCompleteAt: Date?

  init(order: Int, name: String, reps: Int, resistance: Double, resistanceUnit: String, markedCompleteAt: Date?) {
    self.order = order
    self.name = name
    self.reps = reps
    self.resistance = resistance
    self.resistanceUnit = resistanceUnit
    self.markedCompleteAt = markedCompleteAt
  }

  func hash(into hasher: inout Hasher) {
    hasher.combine(order)
    hasher.combine(name)
    hasher.combine(reps)
    hasher.combine(resistance)
    hasher.combine(resistanceUnit)
    hasher.combine(markedCompleteAt)
  }

  static func == (lhs: EphemeralExerciseSet, rhs: EphemeralExerciseSet) -> Bool {
    return lhs.order == rhs.order &&
      lhs.name == rhs.name &&
      lhs.reps == rhs.reps &&
      lhs.resistance == rhs.resistance &&
      lhs.resistanceUnit == rhs.resistanceUnit &&
      lhs.markedCompleteAt == rhs.markedCompleteAt
  }
}

@Observable class EphemeralWorkout: Workout {
  var workoutId: UUID

  var name: String
  var createdAt: Date
  var updatedAt: Date
  var sets: [EphemeralExerciseSet]

  init(workoutId: UUID = UUID(), name: String = "", createdAt: Date = Date(), updatedAt: Date = Date(), sets: [EphemeralExerciseSet] = []) {
    self.workoutId = workoutId
    self.name = name
    self.createdAt = createdAt
    self.updatedAt = updatedAt
    self.sets = sets
  }

  func hash(into hasher: inout Hasher) {
    hasher.combine(workoutId)
    hasher.combine(name)
    hasher.combine(createdAt)
    hasher.combine(updatedAt)
    hasher.combine(sets)
  }

  static func == (lhs: EphemeralWorkout, rhs: EphemeralWorkout) -> Bool {
    return lhs.workoutId == rhs.workoutId &&
      lhs.name == rhs.name &&
      lhs.createdAt == rhs.createdAt &&
      lhs.updatedAt == rhs.updatedAt &&
      lhs.sets == rhs.sets
  }
}

protocol ExerciseSet: Observable, Hashable, Identifiable {
  var name: String { get set }
  var reps: Int { get set }
  var resistance: Double { get set }
  var resistanceUnit: String { get set }
  var markedCompleteAt: Date? { get set }
  var order: Int { get set }
}

@MainActor
protocol WorkoutPersistence<WorkoutType> {
  associatedtype WorkoutType: Workout
  func insert(_ workout: WorkoutType)
  func copy(_ workout: any Workout) -> WorkoutType
  func delete(_ workout: WorkoutType)
  func copy(_ exerciseSet: any ExerciseSet) -> WorkoutType.WorkoutExerciseSet
  func newSet(name: String, reps: Int, resistance: Double, resistanceUnit: String, order: Int) -> WorkoutType.WorkoutExerciseSet
}

class EphemeralWorkoutPersistence: WorkoutPersistence {
  func insert(_ workout: EphemeralWorkout) {}

  func copy(_ workout: any Workout) -> EphemeralWorkout {
    let originalSets: [any ExerciseSet] = workout.sets
    let setsCopy: [EphemeralExerciseSet] = originalSets.map(copy)
    return EphemeralWorkout(name: workout.name, sets: setsCopy)
  }

  func delete(_ workout: EphemeralWorkout) {}

  func copy(_ exerciseSet: any ExerciseSet) -> EphemeralExerciseSet {
    return EphemeralExerciseSet(
      order: exerciseSet.order,
      name: exerciseSet.name,
      reps: exerciseSet.reps,
      resistance: exerciseSet.resistance,
      resistanceUnit: exerciseSet.resistanceUnit,
      markedCompleteAt: Date()
    )
  }

  func newSet(name: String, reps: Int, resistance: Double, resistanceUnit: String, order: Int) -> EphemeralExerciseSet {
    return EphemeralExerciseSet(order: order, name: name, reps: reps, resistance: resistance, resistanceUnit: resistanceUnit, markedCompleteAt: Date())
  }

  typealias WorkoutType = EphemeralWorkout
}

Stack dump

error: compile command failed due to signal 6 (use -v to see invocation)
Unhandled coercion:
(array_slice_type
  (opened_archetype_type address=0x1202908b8 conforms_to="Contents.(file)[email protected]:83:10" opened_existential_id="F29E82FE-750E-11EF-92AF-DE0800FD984B"
    (interface_type=dependent_member_type assoc_type="Contents.(file)[email protected]:5:18"
      (base=generic_type_param_type depth=0 index=0))
    (opened_existential=existential_type
      (protocol_type decl="Contents.(file)[email protected]:4:10"))))
(lvalue_type
  (array_slice_type
    (existential_type
      (protocol_type decl="Contents.(file)[email protected]:83:10"))))
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0.	Program arguments: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file Contents.swift -target arm64-apple-macosx15.0 -Xllvm -aarch64-use-tbi -enable-objc-interop -stack-check -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.0.sdk -color-diagnostics -new-driver-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-driver -empty-abi-descriptor -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -module-name Contents -disable-clang-spi -target-sdk-version 15.0 -target-sdk-name macosx15.0 -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/local/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -plugin-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/local/lib/swift/host/plugins -o /var/folders/fp/3l06jfz175s6w114bdhcsc1h0000gn/T/TemporaryDirectory.5wpURp/Contents-1.o
1.	Apple Swift version 6.0 (swiftlang-6.0.0.9.10 clang-1600.0.26.2)
2.	Compiling with effective version 5.10
3.	While evaluating request TypeCheckSourceFileRequest(source_file "Contents.swift")
4.	While evaluating request TypeCheckFunctionBodyRequest(Contents.(file)[email protected]:105:8)
5.	While type-checking statement at [Contents.swift:105:57 - line:109:3] RangeText="{
    let originalSets: [any ExerciseSet] = workout.sets
    let setsCopy: [EphemeralExerciseSet] = originalSets.map(copy)
    return EphemeralWorkout(name: workout.name, sets: setsCopy)
  "
6.	While type-checking declaration 0x1501e1770 (at Contents.swift:106:5)
7.	While type-checking expression at [Contents.swift:106:43 - line:106:51] RangeText="workout."
8.	While type-checking-target starting at Contents.swift:106:51
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           0x00000001088770fc llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000108875350 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x00000001088776c8 SignalHandler(int) + 292
3  libsystem_platform.dylib 0x0000000188b00184 _sigtramp + 56
4  libsystem_pthread.dylib  0x0000000188acaf70 pthread_kill + 288
5  libsystem_c.dylib        0x00000001889d7908 abort + 128
6  swift-frontend           0x00000001042e310c (anonymous namespace)::ExprRewriter::coerceToType(swift::Expr*, swift::Type, swift::constraints::ConstraintLocatorBuilder) + 10492
7  swift-frontend           0x00000001042e21ac (anonymous namespace)::ExprRewriter::coerceToType(swift::Expr*, swift::Type, swift::constraints::ConstraintLocatorBuilder) + 6556
8  swift-frontend           0x00000001043024d4 (anonymous namespace)::ExprRewriter::closeExistential(swift::Expr*&, swift::constraints::ConstraintLocatorBuilder, bool) + 596
9  swift-frontend           0x00000001042fb118 (anonymous namespace)::ExprRewriter::buildMemberRef(swift::Expr*, swift::SourceLoc, swift::constraints::SelectedOverload, swift::DeclNameLoc, swift::constraints::ConstraintLocatorBuilder, swift::constraints::ConstraintLocatorBuilder, bool, swift::AccessSemantics) + 9320
10 swift-frontend           0x00000001042eceb8 (anonymous namespace)::ExprRewriter::walkToExprPost(swift::Expr*) + 21120
11 swift-frontend           0x00000001042e4664 (anonymous namespace)::ExprWalker::walkToExprPost(swift::Expr*) + 24
12 swift-frontend           0x00000001042dee40 (anonymous namespace)::ExprWalker::rewriteTarget(swift::constraints::SyntacticElementTarget) + 2748
13 swift-frontend           0x00000001042ddec0 swift::constraints::ConstraintSystem::applySolution(swift::constraints::Solution&, swift::constraints::SyntacticElementTarget) + 7120
14 swift-frontend           0x000000010459d6a8 swift::TypeChecker::typeCheckTarget(swift::constraints::SyntacticElementTarget&, swift::optionset::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 680
15 swift-frontend           0x000000010459d2dc swift::TypeChecker::typeCheckExpression(swift::constraints::SyntacticElementTarget&, swift::optionset::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 416
16 swift-frontend           0x000000010459e918 swift::TypeChecker::typeCheckBinding(swift::Pattern*&, swift::Expr*&, swift::DeclContext*, swift::Type, swift::PatternBindingDecl*, unsigned int, swift::optionset::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 172
17 swift-frontend           0x000000010459eba8 swift::TypeChecker::typeCheckPatternBinding(swift::PatternBindingDecl*, unsigned int, swift::Type, swift::optionset::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 356
18 swift-frontend           0x00000001045f26c8 (anonymous namespace)::DeclChecker::visit(swift::Decl*) + 6940
19 swift-frontend           0x00000001045f0b9c swift::TypeChecker::typeCheckDecl(swift::Decl*) + 152
20 swift-frontend           0x000000010468d97c swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 288
21 swift-frontend           0x000000010468b2bc bool (anonymous namespace)::StmtChecker::typeCheckStmt<swift::BraceStmt>(swift::BraceStmt*&) + 316
22 swift-frontend           0x0000000104689e58 swift::TypeCheckFunctionBodyRequest::evaluate(swift::Evaluator&, swift::AbstractFunctionDecl*) const + 1512
23 swift-frontend           0x0000000104a9a798 swift::TypeCheckFunctionBodyRequest::OutputType swift::Evaluator::getResultUncached<swift::TypeCheckFunctionBodyRequest, swift::TypeCheckFunctionBodyRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckFunctionBodyRequest>(swift::Evaluator&, swift::TypeCheckFunctionBodyRequest, swift::TypeCheckFunctionBodyRequest::OutputType)::'lambda'()>(swift::TypeCheckFunctionBodyRequest const&, swift::TypeCheckFunctionBodyRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckFunctionBodyRequest>(swift::Evaluator&, swift::TypeCheckFunctionBodyRequest, swift::TypeCheckFunctionBodyRequest::OutputType)::'lambda'()) + 636
24 swift-frontend           0x0000000104a09308 swift::AbstractFunctionDecl::getTypecheckedBody() const + 160
25 swift-frontend           0x00000001046d7e2c swift::TypeCheckSourceFileRequest::evaluate(swift::Evaluator&, swift::SourceFile*) const + 868
26 swift-frontend           0x00000001046df3c0 swift::TypeCheckSourceFileRequest::OutputType swift::Evaluator::getResultUncached<swift::TypeCheckSourceFileRequest, swift::TypeCheckSourceFileRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckSourceFileRequest>(swift::Evaluator&, swift::TypeCheckSourceFileRequest, swift::TypeCheckSourceFileRequest::OutputType)::'lambda'()>(swift::TypeCheckSourceFileRequest const&, swift::TypeCheckSourceFileRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckSourceFileRequest>(swift::Evaluator&, swift::TypeCheckSourceFileRequest, swift::TypeCheckSourceFileRequest::OutputType)::'lambda'()) + 620
27 swift-frontend           0x00000001046d7aac swift::performTypeChecking(swift::SourceFile&) + 328
28 swift-frontend           0x00000001035f6ce0 swift::CompilerInstance::performSema() + 260
29 swift-frontend           0x0000000103224190 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1528
30 swift-frontend           0x0000000103222f58 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3572
31 swift-frontend           0x00000001031aa01c swift::mainEntry(int, char const**) + 3680
32 dyld                     0x0000000188748274 start + 2840

Expected behavior

The code should compile, or if the language standard has changed in an incompatible way it should produce an error message.

Environment

$ swiftc -version
swift-driver version: 1.115 Apple Swift version 6.0 (swiftlang-6.0.0.9.10 clang-1600.0.26.2)
Target: arm64-apple-macosx15.0

Additional information

No response

@ed-irl ed-irl added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software triage needed This issue needs more specific labels labels Sep 17, 2024
@xedin xedin added type checker Area → compiler: Semantic analysis expressions Feature: expressions implicit existential opening Feature → existentials: implicit opening of existentials when passed to parameters of generic type and removed triage needed This issue needs more specific labels labels Sep 17, 2024
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. crash Bug: A crash, i.e., an abnormal termination of software expressions Feature: expressions implicit existential opening Feature → existentials: implicit opening of existentials when passed to parameters of generic type type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

2 participants