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

"Stored value type does not match pointer operand type!" compiler crash #59572

Open
LeoNatan opened this issue Jun 19, 2022 · 25 comments · Fixed by #59535
Open

"Stored value type does not match pointer operand type!" compiler crash #59572

LeoNatan opened this issue Jun 19, 2022 · 25 comments · Fixed by #59535
Labels
assertion failure Bug → crash: An assertion failure bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. CodeGen compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software generics Feature: generic declarations and types name shadowing Feature: name shadowing patterns Feature: patterns swift 5.5

Comments

@LeoNatan
Copy link

Describe the bug
The following function crashes the compiler:

func setEnumOverrideIfExists<T: RawRepresentable>(for key: String, in override: [String: Any], target: inout T) where T.RawValue == UInt {
    if let overrideValue = override[key] as? UInt, let overrideValue = T(rawValue: overrideValue) {
        target = overrideValue
    }
}

If, however, I make the following change, there is no crash:

func setEnumOverrideIfExists<T: RawRepresentable>(for key: String, in override: [String: Any], target: inout T) where T.RawValue == UInt {
    if let overrideValue = override[key] as? UInt, let overrideValueEnum = T(rawValue: overrideValue) {
        target = overrideValueEnum
    }
}

To Reproduce
Steps to reproduce the behavior:

  1. Add the function above in a Swift file
  2. Compile

Expected behavior
Compiler should not crash.

Environment (please complete the following information):

  • OS: macOS 12.5 (21G5046c)
  • Xcode Version/Tag/Branch: Xcode 13.2

Additional context

Stored value type does not match pointer operand type!
  store i64 %95, i8** %9, align 8, !dbg !393
 i8*in function $s6Panels22PrototypeOverrideModelV07setEnumC8IfExists3for2in0E0ySS_SDySSypGxztSYRzSu8RawValueRtzlF
<unknown>:0: error: fatal error encountered during compilation; please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project
<unknown>:0: note: Broken function found, compilation aborted!
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.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -c /Users/lnatan/Desktop/Code/craft-manager-mac/Submodules/craft-panels/Panels/Panels/Plugins/Sync/Prototyping/PrototypeOverridesEditors.swift -primary-file /Users/lnatan/Desktop/Code/craft-manager-mac/Submodules/craft-panels/Panels/Panels/Plugins/Sync/Prototyping/Models/PrototypeOverrideModel.swift /Users/lnatan/Desktop/Code/craft-manager-mac/Submodules/craft-panels/Panels/Panels/Plugins/Sync/Prototyping/SwiftUIUtils.swift -emit-module-path /Users/lnatan/Library/Developer/Xcode/DerivedData/Panels-eduudpvkojkgnobxrswznjeogchl/Build/Intermediates.noindex/Panels.build/Debug/Panels.build/Objects-normal/x86_64/PrototypeOverrideModel~partial.swiftmodule -emit-module-doc-path /Users/lnatan/Library/Developer/Xcode/DerivedData/Panels-eduudpvkojkgnobxrswznjeogchl/Build/Intermediates.noindex/Panels.build/Debug/Panels.build/Objects-normal/x86_64/PrototypeOverrideModel~partial.swiftdoc -emit-module-source-info-path /Users/lnatan/Library/Developer/Xcode/DerivedData/Panels-eduudpvkojkgnobxrswznjeogchl/Build/Intermediates.noindex/Panels.build/Debug/Panels.build/Objects-normal/x86_64/PrototypeOverrideModel~partial.swiftsourceinfo -emit-dependencies-path /Users/lnatan/Library/Developer/Xcode/DerivedData/Panels-eduudpvkojkgnobxrswznjeogchl/Build/Intermediates.noindex/Panels.build/Debug/Panels.build/Objects-normal/x86_64/PrototypeOverrideModel.d -emit-reference-dependencies-path /Users/lnatan/Library/Developer/Xcode/DerivedData/Panels-eduudpvkojkgnobxrswznjeogchl/Build/Intermediates.noindex/Panels.build/Debug/Panels.build/Objects-normal/x86_64/PrototypeOverrideModel.swiftdeps -serialize-diagnostics-path /Users/lnatan/Library/Developer/Xcode/DerivedData/Panels-eduudpvkojkgnobxrswznjeogchl/Build/Intermediates.noindex/Panels.build/Debug/Panels.build/Objects-normal/x86_64/PrototypeOverrideModel.dia -target x86_64-apple-macos11.0 -enable-objc-interop -stack-check -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk -I /Users/lnatan/Library/Developer/Xcode/DerivedData/Panels-eduudpvkojkgnobxrswznjeogchl/Build/Products/Debug -F /Users/lnatan/Library/Developer/Xcode/DerivedData/Panels-eduudpvkojkgnobxrswznjeogchl/Build/Products/Debug -enable-testing -g -import-underlying-module -module-cache-path /Users/lnatan/Library/Developer/Xcode/DerivedData/ModuleCache.noindex -swift-version 5 -enforce-exclusivity=checked -Onone -new-driver-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-driver -serialize-debugging-options -Xcc -working-directory -Xcc /Users/lnatan/Desktop/Code/craft-manager-mac/Submodules/craft-panels/Panels -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -Xcc -I/Users/lnatan/Library/Developer/Xcode/DerivedData/Panels-eduudpvkojkgnobxrswznjeogchl/Build/Intermediates.noindex/Panels.build/Debug/Panels.build/swift-overrides.hmap -Xcc -iquote -Xcc /Users/lnatan/Library/Developer/Xcode/DerivedData/Panels-eduudpvkojkgnobxrswznjeogchl/Build/Intermediates.noindex/Panels.build/Debug/Panels.build/Panels-generated-files.hmap -Xcc -I/Users/lnatan/Library/Developer/Xcode/DerivedData/Panels-eduudpvkojkgnobxrswznjeogchl/Build/Intermediates.noindex/Panels.build/Debug/Panels.build/Panels-own-target-headers.hmap -Xcc -I/Users/lnatan/Library/Developer/Xcode/DerivedData/Panels-eduudpvkojkgnobxrswznjeogchl/Build/Intermediates.noindex/Panels.build/Debug/Panels.build/Panels-all-non-framework-target-headers.hmap -Xcc -ivfsoverlay -Xcc /Users/lnatan/Library/Developer/Xcode/DerivedData/Panels-eduudpvkojkgnobxrswznjeogchl/Build/Intermediates.noindex/Panels.build/Debug/Panels.build/all-product-headers.yaml -Xcc -iquote -Xcc /Users/lnatan/Library/Developer/Xcode/DerivedData/Panels-eduudpvkojkgnobxrswznjeogchl/Build/Intermediates.noindex/Panels.build/Debug/Panels.build/Panels-project-headers.hmap -Xcc -I/Users/lnatan/Library/Developer/Xcode/DerivedData/Panels-eduudpvkojkgnobxrswznjeogchl/Build/Products/Debug/include -Xcc -I/Users/lnatan/Desktop/Code/craft-manager-mac/Submodules/craft-panels/Panels/Pods/Headers/Public -Xcc -I/Users/lnatan/Desktop/Code/craft-manager-mac/Submodules/craft-panels/Panels/Pods/Headers/Public/CocoaLumberjack -Xcc -I/Users/lnatan/Desktop/Code/craft-manager-mac/Submodules/craft-panels/Panels/Pods/Headers/Public/RNCryptor-objc -Xcc -I/Users/lnatan/Library/Developer/Xcode/DerivedData/Panels-eduudpvkojkgnobxrswznjeogchl/Build/Intermediates.noindex/Panels.build/Debug/Panels.build/DerivedSources-normal/x86_64 -Xcc -I/Users/lnatan/Library/Developer/Xcode/DerivedData/Panels-eduudpvkojkgnobxrswznjeogchl/Build/Intermediates.noindex/Panels.build/Debug/Panels.build/DerivedSources/x86_64 -Xcc -I/Users/lnatan/Library/Developer/Xcode/DerivedData/Panels-eduudpvkojkgnobxrswznjeogchl/Build/Intermediates.noindex/Panels.build/Debug/Panels.build/DerivedSources -Xcc -DDEBUG=1 -Xcc -DCURRENT_USER=@\"lnatan\" -Xcc -DCOCOAPODS=1 -Xcc -ivfsoverlay -Xcc /Users/lnatan/Library/Developer/Xcode/DerivedData/Panels-eduudpvkojkgnobxrswznjeogchl/Build/Intermediates.noindex/Panels.build/Debug/Panels.build/unextended-module-overlay.yaml -module-name Panels -target-sdk-version 12.1.0 -o /Users/lnatan/Library/Developer/Xcode/DerivedData/Panels-eduudpvkojkgnobxrswznjeogchl/Build/Intermediates.noindex/Panels.build/Debug/Panels.build/Objects-normal/x86_64/PrototypeOverrideModel.o -index-store-path /Users/lnatan/Library/Developer/Xcode/DerivedData/Panels-eduudpvkojkgnobxrswznjeogchl/Index/DataStore -index-system-modules
1.	Apple Swift version 5.5.2 (swiftlang-1300.0.47.5 clang-1300.0.29.30)
2.	
3.	Running pass 'Module Verifier' on function '@"$s6Panels22PrototypeOverrideModelV07setEnumC8IfExists3for2in0E0ySS_SDySSypGxztSYRzSu8RawValueRtzlF"'
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           0x000000010f1d7c27 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 39
1  swift-frontend           0x000000010f1d6bb8 llvm::sys::RunSignalHandlers() + 248
2  swift-frontend           0x000000010f1d8236 SignalHandler(int) + 278
3  libsystem_platform.dylib 0x00007ff80790adfd _sigtramp + 29
4  libsystem_platform.dylib 0x0000000000000003 _sigtramp + 18446603370454012451
5  libsystem_c.dylib        0x00007ff807840d24 abort + 123
6  swift-frontend           0x000000010a2e9502 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*)::$_2::__invoke(void*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, bool) + 978
7  swift-frontend           0x000000010f12169e llvm::report_fatal_error(llvm::Twine const&, bool) + 286
8  swift-frontend           0x000000010f12157b llvm::report_fatal_error(char const*, bool) + 43
9  swift-frontend           0x000000010f0b815f (anonymous namespace)::VerifierLegacyPass::runOnFunction(llvm::Function&) + 111
10 swift-frontend           0x000000010f05236a llvm::FPPassManager::runOnFunction(llvm::Function&) + 1354
11 swift-frontend           0x000000010f051651 llvm::legacy::FunctionPassManagerImpl::run(llvm::Function&) + 113
12 swift-frontend           0x000000010f058b75 llvm::legacy::FunctionPassManager::run(llvm::Function&) + 341
13 swift-frontend           0x000000010a7deb61 swift::performLLVMOptimizations(swift::IRGenOptions const&, llvm::Module*, llvm::TargetMachine*) + 1585
14 swift-frontend           0x000000010a7df9c7 swift::performLLVM(swift::IRGenOptions const&, swift::DiagnosticEngine&, llvm::sys::SmartMutex<false>*, llvm::GlobalVariable*, llvm::Module*, llvm::TargetMachine*, llvm::StringRef, swift::UnifiedStatsReporter*) + 2055
15 swift-frontend           0x000000010a2f4a9d performCompileStepsPostSILGen(swift::CompilerInstance&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::PrimarySpecificPaths const&, int&, swift::FrontendObserver*) + 3581
16 swift-frontend           0x000000010a2e6346 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 13830
17 swift-frontend           0x000000010a226b08 main + 1032
18 dyld                     0x000000011c3fa52e start + 462
error: Abort trap: 6 (in target 'Panels' from project 'Panels')
@LeoNatan LeoNatan added the bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. label Jun 19, 2022
@LeoNatan
Copy link
Author

LeoNatan commented Jun 19, 2022

This also compiles without crashing the compiler:

func setEnumOverrideIfExists<T: RawRepresentable, U>(for key: String, in override: [String: Any], target: inout T) where T.RawValue == U {
    if let overrideValue = override[key] as? U, let overrideValue = T(rawValue: overrideValue) {
        target = overrideValue
    }
}

@LeoNatan LeoNatan changed the title "Stored value type does not match pointer operand type!" compile crash "Stored value type does not match pointer operand type!" compiler crash Jun 19, 2022
@AnthonyLatsis
Copy link
Collaborator

Could not reproduce with ToT. May I ask that you isolate the following simplification and see whether it crashes in the terminal with swiftc path/to/file.swift -emit-ir?

func foo<T: RawRepresentable>(src: Any, target: inout T) where T.RawValue == UInt {
  if let x = src as? UInt, let x = T(rawValue: x) {
    target = x
  }
}

@xedin do we actually allow this kind of shadowing?

@LeoNatan
Copy link
Author

@AnthonyLatsis Reproduced with Xcode 13.2 toolchain:

➜  Desktop swiftc test.swift -emit-ir
Stored value type does not match pointer operand type!
  store i64 %68, i8** %overrideValue.debug, align 8
 i8*in function $s4test23setEnumOverrideIfExists3for2in6targetySS_SDySSypGxztSYRzSu8RawValueRtzlF
<unknown>:0: error: fatal error encountered during compilation; please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project
<unknown>:0: note: Broken function found, compilation aborted!
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.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -emit-ir -primary-file test.swift -target x86_64-apple-macosx12.0 -enable-objc-interop -stack-check -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk -color-diagnostics -new-driver-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-driver -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -module-name test -target-sdk-version 12.1.0 -o -
1.	Apple Swift version 5.5.2 (swiftlang-1300.0.47.5 clang-1300.0.29.30)
2.	
3.	Running pass 'Module Verifier' on function '@"$s4test23setEnumOverrideIfExists3for2in6targetySS_SDySSypGxztSYRzSu8RawValueRtzlF"'
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           0x0000000109c41c27 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 39
1  swift-frontend           0x0000000109c40bb8 llvm::sys::RunSignalHandlers() + 248
2  swift-frontend           0x0000000109c42236 SignalHandler(int) + 278
3  libsystem_platform.dylib 0x00007ff80790adfd _sigtramp + 29
4  libsystem_platform.dylib 000000000000000000 _sigtramp + 18446603370454012448
5  libsystem_c.dylib        0x00007ff807840d24 abort + 123
6  swift-frontend           0x0000000104d53502 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*)::$_2::__invoke(void*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, bool) + 978
7  swift-frontend           0x0000000109b8b69e llvm::report_fatal_error(llvm::Twine const&, bool) + 286
8  swift-frontend           0x0000000109b8b57b llvm::report_fatal_error(char const*, bool) + 43
9  swift-frontend           0x0000000109b2215f (anonymous namespace)::VerifierLegacyPass::runOnFunction(llvm::Function&) + 111
10 swift-frontend           0x0000000109abc36a llvm::FPPassManager::runOnFunction(llvm::Function&) + 1354
11 swift-frontend           0x0000000109abb651 llvm::legacy::FunctionPassManagerImpl::run(llvm::Function&) + 113
12 swift-frontend           0x0000000109ac2b75 llvm::legacy::FunctionPassManager::run(llvm::Function&) + 341
13 swift-frontend           0x0000000105248b61 swift::performLLVMOptimizations(swift::IRGenOptions const&, llvm::Module*, llvm::TargetMachine*) + 1585
14 swift-frontend           0x00000001052499c7 swift::performLLVM(swift::IRGenOptions const&, swift::DiagnosticEngine&, llvm::sys::SmartMutex<false>*, llvm::GlobalVariable*, llvm::Module*, llvm::TargetMachine*, llvm::StringRef, swift::UnifiedStatsReporter*) + 2055
15 swift-frontend           0x0000000104d5ea9d performCompileStepsPostSILGen(swift::CompilerInstance&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::PrimarySpecificPaths const&, int&, swift::FrontendObserver*) + 3581
16 swift-frontend           0x0000000104d50346 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 13830
17 swift-frontend           0x0000000104c90b08 main + 1032
18 dyld                     0x000000010dbfb52e start + 462
[1]    47745 abort      swiftc test.swift -emit-ir

@LeoNatan
Copy link
Author

Hm, doesn't reproduce with the toolchain in Xcode 14.0 b1. I guess we can close the issue.

➜  Desktop swiftc test.swift -emit-ir
2022-06-20 19:37:24.373 xcodebuild[47889:1008416] [MT] DVTSDK: Warning: SDK path collision for path '<DVTFilePath:0x6000028fa060:'/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.sdk'>': SDK with name 'macosx13.0' being superseded by SDK with 'macosx13.0'.
2022-06-20 19:37:25.581 xcodebuild[47890:1008438] [MT] DVTSDK: Warning: SDK path collision for path '<DVTFilePath:0x600002a64070:'/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.sdk'>': SDK with name 'macosx13.0' being superseded by SDK with 'macosx13.0'.
; ModuleID = '<swift-imported-modules>'
source_filename = "<swift-imported-modules>"
target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-apple-macosx12.0.0"

%swift.type = type { i64 }
%swift.full_type = type { i8**, %swift.type }
%swift.bridge = type opaque
%swift.opaque = type opaque
%TSS = type <{ %Ts11_StringGutsV }>
%Ts11_StringGutsV = type <{ %Ts13_StringObjectV }>
%Ts13_StringObjectV = type <{ %Ts6UInt64V, %swift.bridge* }>
%Ts6UInt64V = type <{ i64 }>
%TypSg = type <{ [32 x i8] }>
%TSu = type <{ i64 }>
%swift.metadata_response = type { %swift.type*, i64 }
%swift.vwtable = type { i8*, i8*, i8*, i8*, i8*, i8*, i8*, i8*, i64, i64, i32, i32 }
%TSq = type <{}>
%TSq.0 = type <{}>
%Any = type { [24 x i8], %swift.type* }
%TSq.1 = type <{}>
%__opaque_existential_type_0 = type { [24 x i8], %swift.type* }
%swift.refcounted = type { %swift.type*, i64 }

@"$sSSN" = external global %swift.type, align 8
@"$sypN" = external global %swift.full_type
@"$sSSSHsWP" = external global i8*, align 8
@"$sSuN" = external global %swift.type, align 8
@"\01l_entry_point" = private constant { i32 } { i32 trunc (i64 sub (i64 ptrtoint (i32 (i32, i8**)* @main to i64), i64 ptrtoint ({ i32 }* @"\01l_entry_point" to i64)) to i32) }, section "__TEXT, __swift5_entry, regular, no_dead_strip", align 4
@__swift_reflection_version = linkonce_odr hidden constant i16 3
@llvm.used = appending global [3 x i8*] [i8* bitcast (i32 (i32, i8**)* @main to i8*), i8* bitcast ({ i32 }* @"\01l_entry_point" to i8*), i8* bitcast (i16* @__swift_reflection_version to i8*)], section "llvm.metadata"

define i32 @main(i32 %0, i8** %1) #0 {
entry:
  %2 = bitcast i8** %1 to i8*
  ret i32 0
}

define hidden swiftcc void @"$s4test23setEnumOverrideIfExists3for2in6targetySS_SDySSypGxztSYRzSu8RawValueRtzlF"(i64 %0, %swift.bridge* %1, %swift.bridge* %2, %swift.opaque* nocapture %3, %swift.type* %T, i8** %T.RawRepresentable) #0 {
entry:
  %T1 = alloca %swift.type*, align 8
  %overrideValue.debug = alloca i8*, align 8
  %4 = bitcast i8** %overrideValue.debug to i8*
  call void @llvm.memset.p0i8.i64(i8* align 8 %4, i8 0, i64 8, i1 false)
  %key.debug = alloca %TSS, align 8
  %5 = bitcast %TSS* %key.debug to i8*
  call void @llvm.memset.p0i8.i64(i8* align 8 %5, i8 0, i64 16, i1 false)
  %override.debug = alloca %swift.bridge*, align 8
  %6 = bitcast %swift.bridge** %override.debug to i8*
  call void @llvm.memset.p0i8.i64(i8* align 8 %6, i8 0, i64 8, i1 false)
  %target.debug = alloca %swift.opaque*, align 8
  %7 = bitcast %swift.opaque** %target.debug to i8*
  call void @llvm.memset.p0i8.i64(i8* align 8 %7, i8 0, i64 8, i1 false)
  %8 = alloca %TypSg, align 8
  %9 = alloca %TSS, align 8
  %overrideValue.debug3 = alloca i64, align 8
  %10 = bitcast i64* %overrideValue.debug3 to i8*
  call void @llvm.memset.p0i8.i64(i8* align 8 %10, i8 0, i64 8, i1 false)
  %11 = alloca %TSu, align 8
  %12 = alloca %TSu, align 8
  store %swift.type* %T, %swift.type** %T1, align 8
  %13 = call swiftcc %swift.metadata_response @"$sSqMa"(i64 0, %swift.type* %T) #5
  %14 = extractvalue %swift.metadata_response %13, 0
  %15 = bitcast %swift.type* %14 to i8***
  %16 = getelementptr inbounds i8**, i8*** %15, i64 -1
  %.valueWitnesses = load i8**, i8*** %16, align 8, !invariant.load !18, !dereferenceable !19
  %17 = bitcast i8** %.valueWitnesses to %swift.vwtable*
  %18 = getelementptr inbounds %swift.vwtable, %swift.vwtable* %17, i32 0, i32 8
  %size = load i64, i64* %18, align 8, !invariant.load !18
  %19 = alloca i8, i64 %size, align 16
  call void @llvm.lifetime.start.p0i8(i64 -1, i8* %19)
  %20 = bitcast i8* %19 to %TSq*
  %21 = bitcast %swift.type* %T to i8***
  %22 = getelementptr inbounds i8**, i8*** %21, i64 -1
  %T.valueWitnesses = load i8**, i8*** %22, align 8, !invariant.load !18, !dereferenceable !19
  %23 = bitcast i8** %T.valueWitnesses to %swift.vwtable*
  %24 = getelementptr inbounds %swift.vwtable, %swift.vwtable* %23, i32 0, i32 8
  %size2 = load i64, i64* %24, align 8, !invariant.load !18
  %25 = alloca i8, i64 %size2, align 16
  call void @llvm.lifetime.start.p0i8(i64 -1, i8* %25)
  %26 = bitcast i8* %25 to %swift.opaque*
  %27 = alloca i8, i64 %size2, align 16
  call void @llvm.lifetime.start.p0i8(i64 -1, i8* %27)
  %28 = bitcast i8* %27 to %swift.opaque*
  store i8* %27, i8** %overrideValue.debug, align 8
  %29 = bitcast %TSS* %key.debug to i8*
  call void @llvm.lifetime.start.p0i8(i64 16, i8* %29)
  %key.debug._guts = getelementptr inbounds %TSS, %TSS* %key.debug, i32 0, i32 0
  %key.debug._guts._object = getelementptr inbounds %Ts11_StringGutsV, %Ts11_StringGutsV* %key.debug._guts, i32 0, i32 0
  %key.debug._guts._object._countAndFlagsBits = getelementptr inbounds %Ts13_StringObjectV, %Ts13_StringObjectV* %key.debug._guts._object, i32 0, i32 0
  %key.debug._guts._object._countAndFlagsBits._value = getelementptr inbounds %Ts6UInt64V, %Ts6UInt64V* %key.debug._guts._object._countAndFlagsBits, i32 0, i32 0
  store i64 %0, i64* %key.debug._guts._object._countAndFlagsBits._value, align 8
  %key.debug._guts._object._object = getelementptr inbounds %Ts13_StringObjectV, %Ts13_StringObjectV* %key.debug._guts._object, i32 0, i32 1
  store %swift.bridge* %1, %swift.bridge** %key.debug._guts._object._object, align 8
  store %swift.bridge* %2, %swift.bridge** %override.debug, align 8
  store %swift.opaque* %3, %swift.opaque** %target.debug, align 8
  %30 = bitcast %TypSg* %8 to i8*
  call void @llvm.lifetime.start.p0i8(i64 32, i8* %30)
  %31 = call %swift.bridge* @swift_bridgeObjectRetain(%swift.bridge* returned %1) #3
  %32 = bitcast %TSS* %9 to i8*
  call void @llvm.lifetime.start.p0i8(i64 16, i8* %32)
  %._guts = getelementptr inbounds %TSS, %TSS* %9, i32 0, i32 0
  %._guts._object = getelementptr inbounds %Ts11_StringGutsV, %Ts11_StringGutsV* %._guts, i32 0, i32 0
  %._guts._object._countAndFlagsBits = getelementptr inbounds %Ts13_StringObjectV, %Ts13_StringObjectV* %._guts._object, i32 0, i32 0
  %._guts._object._countAndFlagsBits._value = getelementptr inbounds %Ts6UInt64V, %Ts6UInt64V* %._guts._object._countAndFlagsBits, i32 0, i32 0
  store i64 %0, i64* %._guts._object._countAndFlagsBits._value, align 8
  %._guts._object._object = getelementptr inbounds %Ts13_StringObjectV, %Ts13_StringObjectV* %._guts._object, i32 0, i32 1
  store %swift.bridge* %1, %swift.bridge** %._guts._object._object, align 8
  %33 = bitcast %TypSg* %8 to %TSq.0*
  %34 = bitcast %TSS* %9 to %swift.opaque*
  %35 = bitcast %TSq.0* %33 to %swift.opaque*
  call swiftcc void @"$sSDyq_Sgxcig"(%swift.opaque* noalias nocapture sret(%swift.opaque) %35, %swift.opaque* noalias nocapture %34, %swift.bridge* %2, %swift.type* @"$sSSN", %swift.type* getelementptr inbounds (%swift.full_type, %swift.full_type* @"$sypN", i32 0, i32 1), i8** @"$sSSSHsWP")
  %36 = call %TSS* @"$sSSWOh"(%TSS* %9)
  %37 = bitcast %TSS* %9 to i8*
  call void @llvm.lifetime.end.p0i8(i64 16, i8* %37)
  %38 = bitcast %TypSg* %8 to { i64, i64, i64, i64 }*
  %39 = getelementptr inbounds { i64, i64, i64, i64 }, { i64, i64, i64, i64 }* %38, i32 0, i32 0
  %40 = load i64, i64* %39, align 8
  %41 = getelementptr inbounds { i64, i64, i64, i64 }, { i64, i64, i64, i64 }* %38, i32 0, i32 1
  %42 = load i64, i64* %41, align 8
  %43 = getelementptr inbounds { i64, i64, i64, i64 }, { i64, i64, i64, i64 }* %38, i32 0, i32 2
  %44 = load i64, i64* %43, align 8
  %45 = getelementptr inbounds { i64, i64, i64, i64 }, { i64, i64, i64, i64 }* %38, i32 0, i32 3
  %46 = load i64, i64* %45, align 8
  %47 = icmp eq i64 %46, 0
  br i1 %47, label %96, label %48

48:                                               ; preds = %entry
  %49 = bitcast %TypSg* %8 to %Any*
  %50 = bitcast %TSu* %12 to i8*
  call void @llvm.lifetime.start.p0i8(i64 8, i8* %50)
  %51 = bitcast %TSu* %12 to %swift.opaque*
  %52 = bitcast %Any* %49 to %swift.opaque*
  %53 = call i1 @swift_dynamicCast(%swift.opaque* %51, %swift.opaque* %52, %swift.type* getelementptr inbounds (%swift.full_type, %swift.full_type* @"$sypN", i32 0, i32 1), %swift.type* @"$sSuN", i64 6) #3
  br i1 %53, label %54, label %57

54:                                               ; preds = %48
  %._value4 = getelementptr inbounds %TSu, %TSu* %12, i32 0, i32 0
  %55 = load i64, i64* %._value4, align 8
  %56 = bitcast %TSu* %12 to i8*
  call void @llvm.lifetime.end.p0i8(i64 8, i8* %56)
  br label %59

57:                                               ; preds = %48
  %58 = bitcast %TSu* %12 to i8*
  call void @llvm.lifetime.end.p0i8(i64 8, i8* %58)
  br label %59

59:                                               ; preds = %54, %57
  %60 = phi i64 [ 0, %57 ], [ %55, %54 ]
  %61 = phi i1 [ true, %57 ], [ false, %54 ]
  %62 = bitcast %TypSg* %8 to i8*
  call void @llvm.lifetime.end.p0i8(i64 32, i8* %62)
  br label %63

63:                                               ; preds = %59, %96
  %64 = phi i64 [ 0, %96 ], [ %60, %59 ]
  %65 = phi i1 [ true, %96 ], [ %61, %59 ]
  br i1 %65, label %67, label %66

66:                                               ; preds = %63
  br label %68

67:                                               ; preds = %63
  br label %99

68:                                               ; preds = %66
  %69 = phi i64 [ %64, %66 ]
  store i64 %69, i64* %overrideValue.debug3, align 8
  %70 = bitcast %TSu* %11 to i8*
  call void @llvm.lifetime.start.p0i8(i64 8, i8* %70)
  %._value = getelementptr inbounds %TSu, %TSu* %11, i32 0, i32 0
  store i64 %69, i64* %._value, align 8
  %71 = bitcast %TSq* %20 to %TSq.1*
  %72 = bitcast %TSu* %11 to %swift.opaque*
  %73 = bitcast %TSq.1* %71 to %swift.opaque*
  call swiftcc void @"$sSY8rawValuexSg03RawB0Qz_tcfCTj"(%swift.opaque* noalias nocapture sret(%swift.opaque) %73, %swift.opaque* noalias nocapture %72, %swift.type* swiftself %T, %swift.type* %T, i8** %T.RawRepresentable)
  %74 = bitcast %TSu* %11 to i8*
  call void @llvm.lifetime.end.p0i8(i64 8, i8* %74)
  %75 = bitcast %TSq* %20 to %swift.opaque*
  %76 = getelementptr inbounds i8*, i8** %T.valueWitnesses, i32 6
  %77 = load i8*, i8** %76, align 8, !invariant.load !18
  %getEnumTagSinglePayload = bitcast i8* %77 to i32 (%swift.opaque*, i32, %swift.type*)*
  %78 = call i32 %getEnumTagSinglePayload(%swift.opaque* noalias %75, i32 1, %swift.type* %T) #6
  %79 = icmp ne i32 %78, 1
  br i1 %79, label %83, label %81

80:                                               ; No predecessors!
  unreachable

81:                                               ; preds = %68
  %82 = call %TSq* @"$sxSgSYRzSu8RawValueRtzlWOh"(%TSq* %20, %swift.type* %T, %swift.type* %14)
  br label %99

83:                                               ; preds = %68
  %84 = bitcast %TSq* %20 to %swift.opaque*
  %85 = getelementptr inbounds i8*, i8** %T.valueWitnesses, i32 4
  %86 = load i8*, i8** %85, align 8, !invariant.load !18
  %initializeWithTake = bitcast i8* %86 to %swift.opaque* (%swift.opaque*, %swift.opaque*, %swift.type*)*
  %87 = call %swift.opaque* %initializeWithTake(%swift.opaque* noalias %28, %swift.opaque* noalias %84, %swift.type* %T) #3
  %88 = getelementptr inbounds i8*, i8** %T.valueWitnesses, i32 2
  %89 = load i8*, i8** %88, align 8, !invariant.load !18
  %initializeWithCopy = bitcast i8* %89 to %swift.opaque* (%swift.opaque*, %swift.opaque*, %swift.type*)*
  %90 = call %swift.opaque* %initializeWithCopy(%swift.opaque* noalias %26, %swift.opaque* noalias %28, %swift.type* %T) #3
  %91 = getelementptr inbounds i8*, i8** %T.valueWitnesses, i32 5
  %92 = load i8*, i8** %91, align 8, !invariant.load !18
  %assignWithTake = bitcast i8* %92 to %swift.opaque* (%swift.opaque*, %swift.opaque*, %swift.type*)*
  %93 = call %swift.opaque* %assignWithTake(%swift.opaque* noalias %3, %swift.opaque* noalias %26, %swift.type* %T) #3
  %94 = getelementptr inbounds i8*, i8** %T.valueWitnesses, i32 1
  %95 = load i8*, i8** %94, align 8, !invariant.load !18
  %destroy = bitcast i8* %95 to void (%swift.opaque*, %swift.type*)*
  call void %destroy(%swift.opaque* noalias %28, %swift.type* %T) #3
  br label %99

96:                                               ; preds = %entry
  %97 = call %TypSg* @"$sypSgWOh"(%TypSg* %8)
  %98 = bitcast %TypSg* %8 to i8*
  call void @llvm.lifetime.end.p0i8(i64 32, i8* %98)
  br label %63

99:                                               ; preds = %83, %81, %67
  %100 = bitcast %swift.opaque* %28 to i8*
  call void @llvm.lifetime.end.p0i8(i64 -1, i8* %100)
  %101 = bitcast %swift.opaque* %26 to i8*
  call void @llvm.lifetime.end.p0i8(i64 -1, i8* %101)
  %102 = bitcast %TSq* %20 to i8*
  call void @llvm.lifetime.end.p0i8(i64 -1, i8* %102)
  ret void
}

declare swiftcc %swift.metadata_response @"$sSqMa"(i64, %swift.type*) #0

; Function Attrs: argmemonly nofree nosync nounwind willreturn
declare void @llvm.lifetime.start.p0i8(i64 immarg, i8* nocapture) #1

; Function Attrs: argmemonly nofree nounwind willreturn writeonly
declare void @llvm.memset.p0i8.i64(i8* nocapture writeonly, i8, i64, i1 immarg) #2

; Function Attrs: nounwind
declare %swift.bridge* @swift_bridgeObjectRetain(%swift.bridge* returned) #3

declare swiftcc void @"$sSDyq_Sgxcig"(%swift.opaque* noalias nocapture sret(%swift.opaque), %swift.opaque* noalias nocapture, %swift.bridge*, %swift.type*, %swift.type*, i8**) #0

; Function Attrs: noinline nounwind
define linkonce_odr hidden %TSS* @"$sSSWOh"(%TSS* %0) #4 {
entry:
  %._guts = getelementptr inbounds %TSS, %TSS* %0, i32 0, i32 0
  %._guts._object = getelementptr inbounds %Ts11_StringGutsV, %Ts11_StringGutsV* %._guts, i32 0, i32 0
  %._guts._object._object = getelementptr inbounds %Ts13_StringObjectV, %Ts13_StringObjectV* %._guts._object, i32 0, i32 1
  %toDestroy = load %swift.bridge*, %swift.bridge** %._guts._object._object, align 8
  call void @swift_bridgeObjectRelease(%swift.bridge* %toDestroy) #3
  ret %TSS* %0
}

; Function Attrs: nounwind
declare void @swift_bridgeObjectRelease(%swift.bridge*) #3

; Function Attrs: argmemonly nofree nosync nounwind willreturn
declare void @llvm.lifetime.end.p0i8(i64 immarg, i8* nocapture) #1

; Function Attrs: noinline nounwind
define linkonce_odr hidden %TypSg* @"$sypSgWOh"(%TypSg* %0) #4 {
entry:
  %1 = bitcast %TypSg* %0 to { i64, i64, i64, i64 }*
  %2 = getelementptr inbounds { i64, i64, i64, i64 }, { i64, i64, i64, i64 }* %1, i32 0, i32 0
  %3 = load i64, i64* %2, align 8
  %4 = getelementptr inbounds { i64, i64, i64, i64 }, { i64, i64, i64, i64 }* %1, i32 0, i32 1
  %5 = load i64, i64* %4, align 8
  %6 = getelementptr inbounds { i64, i64, i64, i64 }, { i64, i64, i64, i64 }* %1, i32 0, i32 2
  %7 = load i64, i64* %6, align 8
  %8 = getelementptr inbounds { i64, i64, i64, i64 }, { i64, i64, i64, i64 }* %1, i32 0, i32 3
  %9 = load i64, i64* %8, align 8
  %10 = icmp eq i64 %9, 0
  br i1 %10, label %14, label %11

11:                                               ; preds = %entry
  %12 = bitcast %TypSg* %0 to %Any*
  %13 = bitcast %Any* %12 to %__opaque_existential_type_0*
  call void @__swift_destroy_boxed_opaque_existential_0(%__opaque_existential_type_0* %13) #3
  br label %14

14:                                               ; preds = %11, %entry
  ret %TypSg* %0
}

; Function Attrs: noinline nounwind
define linkonce_odr hidden void @__swift_destroy_boxed_opaque_existential_0(%__opaque_existential_type_0* %0) #4 {
entry:
  %1 = getelementptr inbounds %__opaque_existential_type_0, %__opaque_existential_type_0* %0, i32 0, i32 1
  %2 = load %swift.type*, %swift.type** %1, align 8
  %3 = getelementptr inbounds %__opaque_existential_type_0, %__opaque_existential_type_0* %0, i32 0, i32 0
  %4 = bitcast %swift.type* %2 to i8***
  %5 = getelementptr inbounds i8**, i8*** %4, i64 -1
  %.valueWitnesses = load i8**, i8*** %5, align 8, !invariant.load !18, !dereferenceable !19
  %6 = bitcast i8** %.valueWitnesses to %swift.vwtable*
  %7 = getelementptr inbounds %swift.vwtable, %swift.vwtable* %6, i32 0, i32 10
  %flags = load i32, i32* %7, align 8, !invariant.load !18
  %8 = and i32 %flags, 131072
  %flags.isInline = icmp eq i32 %8, 0
  br i1 %flags.isInline, label %inline, label %outline

inline:                                           ; preds = %entry
  %9 = bitcast [24 x i8]* %3 to %swift.opaque*
  %10 = bitcast %swift.type* %2 to i8***
  %11 = getelementptr inbounds i8**, i8*** %10, i64 -1
  %.valueWitnesses1 = load i8**, i8*** %11, align 8, !invariant.load !18, !dereferenceable !19
  %12 = getelementptr inbounds i8*, i8** %.valueWitnesses1, i32 1
  %13 = load i8*, i8** %12, align 8, !invariant.load !18
  %destroy = bitcast i8* %13 to void (%swift.opaque*, %swift.type*)*
  call void %destroy(%swift.opaque* noalias %9, %swift.type* %2) #3
  ret void

outline:                                          ; preds = %entry
  %14 = bitcast [24 x i8]* %3 to %swift.refcounted**
  %15 = load %swift.refcounted*, %swift.refcounted** %14, align 8
  call void @swift_release(%swift.refcounted* %15) #3
  ret void
}

; Function Attrs: nounwind
declare void @swift_release(%swift.refcounted*) #3

declare swiftcc void @"$sSY8rawValuexSg03RawB0Qz_tcfCTj"(%swift.opaque* noalias nocapture sret(%swift.opaque), %swift.opaque* noalias nocapture, %swift.type* swiftself, %swift.type*, i8**) #0

; Function Attrs: noinline nounwind
define linkonce_odr hidden %TSq* @"$sxSgSYRzSu8RawValueRtzlWOh"(%TSq* %0, %swift.type* %T, %swift.type* %"Optional<T>") #4 {
entry:
  %T1 = alloca %swift.type*, align 8
  store %swift.type* %T, %swift.type** %T1, align 8
  %1 = bitcast %TSq* %0 to %swift.opaque*
  %2 = bitcast %swift.type* %T to i8***
  %3 = getelementptr inbounds i8**, i8*** %2, i64 -1
  %T.valueWitnesses = load i8**, i8*** %3, align 8, !invariant.load !18, !dereferenceable !19
  %4 = getelementptr inbounds i8*, i8** %T.valueWitnesses, i32 6
  %5 = load i8*, i8** %4, align 8, !invariant.load !18
  %getEnumTagSinglePayload = bitcast i8* %5 to i32 (%swift.opaque*, i32, %swift.type*)*
  %6 = call i32 %getEnumTagSinglePayload(%swift.opaque* noalias %1, i32 1, %swift.type* %T) #6
  %7 = icmp eq i32 %6, 0
  br i1 %7, label %8, label %12

8:                                                ; preds = %entry
  %9 = bitcast %TSq* %0 to %swift.opaque*
  %10 = getelementptr inbounds i8*, i8** %T.valueWitnesses, i32 1
  %11 = load i8*, i8** %10, align 8, !invariant.load !18
  %destroy = bitcast i8* %11 to void (%swift.opaque*, %swift.type*)*
  call void %destroy(%swift.opaque* noalias %9, %swift.type* %T) #3
  br label %12

12:                                               ; preds = %8, %entry
  ret %TSq* %0
}

; Function Attrs: nounwind
declare zeroext i1 @swift_dynamicCast(%swift.opaque*, %swift.opaque*, %swift.type*, %swift.type*, i64) #3

attributes #0 = { "frame-pointer"="all" "no-trapping-math"="true" "probe-stack"="__chkstk_darwin" "stack-protector-buffer-size"="8" "target-cpu"="penryn" "target-features"="+cx16,+cx8,+fxsr,+mmx,+sahf,+sse,+sse2,+sse3,+sse4.1,+ssse3,+x87" "tune-cpu"="generic" }
attributes #1 = { argmemonly nofree nosync nounwind willreturn }
attributes #2 = { argmemonly nofree nounwind willreturn writeonly }
attributes #3 = { nounwind }
attributes #4 = { noinline nounwind "frame-pointer"="all" "no-trapping-math"="true" "probe-stack"="__chkstk_darwin" "stack-protector-buffer-size"="8" "target-cpu"="penryn" "target-features"="+cx16,+cx8,+fxsr,+mmx,+sahf,+sse,+sse2,+sse3,+sse4.1,+ssse3,+x87" "tune-cpu"="generic" }
attributes #5 = { nounwind readnone }
attributes #6 = { nounwind readonly }

!llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7, !8, !9, !10, !11}
!swift.module.flags = !{!12}
!llvm.linker.options = !{!13, !14, !15, !16, !17}

!0 = !{i32 2, !"SDK Version", [2 x i32] [i32 13, i32 0]}
!1 = !{i32 1, !"Objective-C Version", i32 2}
!2 = !{i32 1, !"Objective-C Image Info Version", i32 0}
!3 = !{i32 1, !"Objective-C Image Info Section", !"__DATA,__objc_imageinfo,regular,no_dead_strip"}
!4 = !{i32 4, !"Objective-C Garbage Collection", i32 84346624}
!5 = !{i32 1, !"Objective-C Class Properties", i32 64}
!6 = !{i32 1, !"Objective-C Enforce ClassRO Pointer Signing", i8 0}
!7 = !{i32 1, !"wchar_size", i32 4}
!8 = !{i32 7, !"PIC Level", i32 2}
!9 = !{i32 7, !"uwtable", i32 1}
!10 = !{i32 7, !"frame-pointer", i32 2}
!11 = !{i32 1, !"Swift Version", i32 7}
!12 = !{!"standard-library", i1 false}
!13 = !{!"-lswiftSwiftOnoneSupport"}
!14 = !{!"-lswiftCore"}
!15 = !{!"-lswift_Concurrency"}
!16 = !{!"-lswift_StringProcessing"}
!17 = !{!"-lobjc"}
!18 = !{}
!19 = !{i64 88}

@AnthonyLatsis
Copy link
Collaborator

AnthonyLatsis commented Jun 20, 2022

Reproduced with Xcode 13.2 toolchain

Was this the original example or the one I provided?


I would like to add a regression test and confirm that the shadowing is supported before we close this for good—we don't want this to suddenly reemerge in a future release.

@AnthonyLatsis AnthonyLatsis reopened this Jun 20, 2022
@LeoNatan
Copy link
Author

Not sure what I ran before, but this is with your x code:

➜  Desktop swiftc test.swift -emit-ir
Stored value type does not match pointer operand type!
  store i64 %41, i8** %x.debug, align 8
 i8*in function $s4test3foo3src6targetyyp_xztSYRzSu8RawValueRtzlF
<unknown>:0: error: fatal error encountered during compilation; please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project
<unknown>:0: note: Broken function found, compilation aborted!
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.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -emit-ir -primary-file test.swift -target x86_64-apple-macosx12.0 -enable-objc-interop -stack-check -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk -color-diagnostics -new-driver-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-driver -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -module-name test -target-sdk-version 12.1.0 -o -
1.	Apple Swift version 5.5.2 (swiftlang-1300.0.47.5 clang-1300.0.29.30)
2.	
3.	Running pass 'Module Verifier' on function '@"$s4test3foo3src6targetyyp_xztSYRzSu8RawValueRtzlF"'
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           0x000000010b8fdc27 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 39
1  swift-frontend           0x000000010b8fcbb8 llvm::sys::RunSignalHandlers() + 248
2  swift-frontend           0x000000010b8fe236 SignalHandler(int) + 278
3  libsystem_platform.dylib 0x00007ff80790adfd _sigtramp + 29
4  libsystem_platform.dylib 000000000000000000 _sigtramp + 18446603370454012448
5  libsystem_c.dylib        0x00007ff807840d24 abort + 123
6  swift-frontend           0x0000000106a0f502 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*)::$_2::__invoke(void*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, bool) + 978
7  swift-frontend           0x000000010b84769e llvm::report_fatal_error(llvm::Twine const&, bool) + 286
8  swift-frontend           0x000000010b84757b llvm::report_fatal_error(char const*, bool) + 43
9  swift-frontend           0x000000010b7de15f (anonymous namespace)::VerifierLegacyPass::runOnFunction(llvm::Function&) + 111
10 swift-frontend           0x000000010b77836a llvm::FPPassManager::runOnFunction(llvm::Function&) + 1354
11 swift-frontend           0x000000010b777651 llvm::legacy::FunctionPassManagerImpl::run(llvm::Function&) + 113
12 swift-frontend           0x000000010b77eb75 llvm::legacy::FunctionPassManager::run(llvm::Function&) + 341
13 swift-frontend           0x0000000106f04b61 swift::performLLVMOptimizations(swift::IRGenOptions const&, llvm::Module*, llvm::TargetMachine*) + 1585
14 swift-frontend           0x0000000106f059c7 swift::performLLVM(swift::IRGenOptions const&, swift::DiagnosticEngine&, llvm::sys::SmartMutex<false>*, llvm::GlobalVariable*, llvm::Module*, llvm::TargetMachine*, llvm::StringRef, swift::UnifiedStatsReporter*) + 2055
15 swift-frontend           0x0000000106a1aa9d performCompileStepsPostSILGen(swift::CompilerInstance&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::PrimarySpecificPaths const&, int&, swift::FrontendObserver*) + 3581
16 swift-frontend           0x0000000106a0c346 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 13830
17 swift-frontend           0x000000010694cb08 main + 1032
18 dyld                     0x00000001185ec52e start + 462
[1]    49512 abort      swiftc test.swift -emit-ir

test.swift.zip

@AnthonyLatsis
Copy link
Collaborator

Great to know we have a reduced reproducer, thanks!

@LeoNatan
Copy link
Author

It might be fixed, the compiler doesn’t crash with the Xcode 14 beta toolchain.

@AnthonyLatsis
Copy link
Collaborator

It no longer crashes, but the rebinding of x looks a bit fishy; I am unaware that we allow this kind of shadowing.

@LeoNatan
Copy link
Author

Well, everywhere else this works as expected. I don’t see why this shadowing wouldn’t be allowed, while shadowing in general is allowed. There is nothing special here.

@AnthonyLatsis
Copy link
Collaborator

AnthonyLatsis commented Jun 20, 2022

There are very few occasions where we allow shadowing within the same lexical scope. This could be one of them, except we seem to lack the test coverage.

AnthonyLatsis added a commit to AnthonyLatsis/swift that referenced this issue Jun 20, 2022
AnthonyLatsis added a commit to AnthonyLatsis/swift that referenced this issue Jun 22, 2022
@LeoNatan
Copy link
Author

Thanks!

@osrufung
Copy link

osrufung commented Jun 9, 2023

This is still happening with Xcode 15 beta and Swift 5.9

@AnthonyLatsis
Copy link
Collaborator

@osrufung Please provide a reproducer. I could not get the original example to crash with a recent revision, and I would not expect it to in Xcode 15 beta either since the regression test had already been added by the time the 5.9 branch was cut.

@AnthonyLatsis AnthonyLatsis added compiler The Swift compiler in itself CodeGen crash Bug: A crash, i.e., an abnormal termination of software swift 5.5 assertion failure Bug → crash: An assertion failure generics Feature: generic declarations and types name shadowing Feature: name shadowing patterns Feature: patterns labels Jun 12, 2023
@insidegui
Copy link

@AnthonyLatsis I happened to run into what I believe to be this same issue in one of my projects when building with Xcode 15 beta 1, and I managed to narrow it down and create a sample project that reproduces it.

Let me know if I should file mine separately, in case it’s not directly related to this one.

Here's the reproduction case:

import SwiftUI

public enum EnumWithAssociatedValues: Hashable, Codable {

    public enum NestedEnum: Int, Codable {
        case success
        case failure
    }

    case caseWithoutValues
    case caseWithNestedOptionalEnum(NestedEnum?)
    /// If this enum case is removed, then the `let result = result` no longer causes a compiler crash.
    case caseWithTwoValues(id: String, value: Double)
}

struct ContentView: View {
    @State private var enumValue: EnumWithAssociatedValues?

    var body: some View {
        VStack {
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundStyle(.tint)
            Text("Hello, world!")
        }
        .padding()
        .onChange(of: enumValue) { newValue in
            /// Note: the compiler crash occurs even if the call is not made within a SwiftUI view modifier block.
            performAction(for: newValue)
        }
    }

    private func performAction(for newValue: EnumWithAssociatedValues?) {
        guard let ring = newValue else { return }

        /// The line below crashes the Swift compiler in Xcode 15 if using `let result = result` and the enum includes the `caseWithTwoValues` case.
        guard case .caseWithNestedOptionalEnum(let result) = ring, let result = result else { return }

        switch result {
        case .success: print("success")
        case .failure: print("failure")
        }
    }
}

Here's the stack dump:

Stored value type does not match pointer operand type!
  store i1 %44, i8* %12, align 8, !dbg !750
 i1Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
1.	Apple Swift version 5.9 (swiftlang-5.9.0.114.6 clang-1500.0.27.1)
2.	Compiling with the current language version
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           0x0000000109eef91c llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000109eeeae0 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000109eeff1c SignalHandler(int) + 352
3  libsystem_platform.dylib 0x00000001a18a6a24 _sigtramp + 56
4  libsystem_pthread.dylib  0x00000001a1877c28 pthread_kill + 288
5  libsystem_c.dylib        0x00000001a1785ae8 abort + 180
6  swift-frontend           0x0000000104928b14 PrettyStackTraceFrontend::~PrettyStackTraceFrontend() + 0
7  swift-frontend           0x0000000109e3c6fc llvm::report_fatal_error(llvm::Twine const&, bool) + 280
8  swift-frontend           0x0000000109e3c5e4 llvm::report_fatal_error(llvm::Twine const&, bool) + 0
9  swift-frontend           0x0000000104e95670 llvm::detail::PassModel<llvm::Module, llvm::VerifierPass, llvm::PreservedAnalyses, llvm::AnalysisManager<llvm::Module>>::printPipeline(llvm::raw_ostream&, llvm::function_ref<llvm::StringRef (llvm::StringRef)>) + 0
10 swift-frontend           0x0000000109bf7064 llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) + 224
11 swift-frontend           0x0000000104e84114 swift::performLLVMOptimizations(swift::IRGenOptions const&, llvm::Module*, llvm::TargetMachine*, llvm::raw_pwrite_stream*) + 4252
12 swift-frontend           0x0000000104e85b20 swift::performLLVM(swift::IRGenOptions const&, swift::DiagnosticEngine&, llvm::sys::SmartMutex<false>*, llvm::GlobalVariable*, llvm::Module*, llvm::TargetMachine*, llvm::StringRef, swift::UnifiedStatsReporter*) + 2736
13 swift-frontend           0x00000001049239dc generateCode(swift::CompilerInstance&, llvm::StringRef, llvm::Module*, llvm::GlobalVariable*) + 252
14 swift-frontend           0x000000010491e108 performCompileStepsPostSILGen(swift::CompilerInstance&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>>, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::PrimarySpecificPaths const&, int&, swift::FrontendObserver*) + 1708
15 swift-frontend           0x0000000104920d48 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1748
16 swift-frontend           0x000000010491f1e0 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 4216
17 swift-frontend           0x00000001048e4a58 swift::mainEntry(int, char const**) + 4112
18 dyld                     0x00000001a151ff28 start + 2236

Here's an Xcode project with the reproduction case: Swift-59572.zip

@osrufung
Copy link

@osrufung Please provide a reproducer. I could not get the original example to crash with a recent revision, and I would not expect it to in Xcode 15 beta either since the regression test had already been added by the time the 5.9 branch was cut.

Here you can find a Swift 5.9 Package that reproduce the issue in Xcode 15

MyLibrary.zip

@RobertDresler
Copy link

Hello, when I try to build the app with external package using Xcode 15 beta 2 (Swift 5.9), it started throwing errors during compiling that package. Do you think it is the same problem?

Screenshot 2023-06-22 at 9 41 22

@AnthonyLatsis
Copy link
Collaborator

AnthonyLatsis commented Jun 22, 2023

I see, you need the -primary-file option for it to crash. Whatever broke this again landed because the regression test does not pass the input file via that option.

func foo<T: RawRepresentable>(src: Any, _: T) where T.RawValue == Int {
  if let x = src as? Int, let x = T(rawValue: x) {}
}
Invalid shadow copy:
  Value :   %36 = phi i64 [ %30, %32 ]
  Alloca:   %x.debug = alloca i8*, align 8
---
Previous shadow copy into x in the same scope!
Scope:
{

   parent: {
  
     parent: {
    /Users/mac/Desktop/test.swift:114:6
       parent: @$s4test3foo3src_yyp_xtSYRzSi8RawValueRtzlF
      }
    
    }
  
  }
Assertion failed: (canAllocaStoreValue(Address, Storage, VarInfo, Scope) && "bad scope?"), function emitShadowCopy, file IRGenSIL.cpp, line 960.
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0.	Program arguments: bin/swift-frontend -primary-file /Users/mac/Desktop/test.swift -emit-ir
1.	Swift version 5.9-dev (LLVM d638e0929fa87b0, Swift 23e2f345b51ca0c)
2.	Compiling with the current language version
3.	While evaluating request IRGenRequest(IR Generation for file "/Users/mac/Desktop/test.swift")
4.	While emitting IR SIL function "@$s4test3foo3src_yyp_xtSYRzSi8RawValueRtzlF".
 for 'foo(src:_:)' (at /Users/mac/Desktop/test.swift:114:1)
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           0x000000010d058e57 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 39
1  swift-frontend           0x000000010d058135 llvm::sys::RunSignalHandlers() + 85
2  swift-frontend           0x000000010d0594a0 SignalHandler(int) + 288
3  libsystem_platform.dylib 0x00007ff815a94dfd _sigtramp + 29
4  libsystem_platform.dylib 000000000000000000 _sigtramp + 18446603370217517600
5  libsystem_c.dylib        0x00007ff8159cad24 abort + 123
6  libsystem_c.dylib        0x00007ff8159ca0cb err + 0
7  swift-frontend           0x000000010d1ac5f3 (anonymous namespace)::IRGenSILFunction::emitShadowCopy(llvm::Value*, swift::SILDebugScope const*, swift::SILDebugVariable, llvm::Optional<swift::irgen::Alignment>, bool, bool) (.cold.5) + 35
8  swift-frontend           0x0000000107a98680 (anonymous namespace)::IRGenSILFunction::emitShadowCopy(llvm::Value*, swift::SILDebugScope const*, swift::SILDebugVariable, llvm::Optional<swift::irgen::Alignment>, bool, bool) + 992
9  swift-frontend           0x0000000107a998d2 (anonymous namespace)::IRGenSILFunction::emitShadowCopyIfNeeded(llvm::Value*, swift::SILDebugScope const*, swift::SILDebugVariable, bool, bool, llvm::Optional<swift::irgen::Alignment>) + 722
10 swift-frontend           0x0000000107a93d2b swift::SILInstructionVisitor<(anonymous namespace)::IRGenSILFunction, void>::visit(swift::SILInstruction*) + 92219
11 swift-frontend           0x0000000107a7859a (anonymous namespace)::IRGenSILFunction::emitSILFunction() + 11210
12 swift-frontend           0x0000000107a75371 swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 1905
13 swift-frontend           0x00000001078e4876 swift::irgen::IRGenerator::emitGlobalTopLevel(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>>> const&) + 886
14 swift-frontend           0x00000001079fdcc4 swift::IRGenRequest::evaluate(swift::Evaluator&, swift::IRGenDescriptor) const + 1796
15 swift-frontend           0x0000000107a7472c swift::GeneratedModule swift::SimpleRequest<swift::IRGenRequest, swift::GeneratedModule (swift::IRGenDescriptor), (swift::RequestFlags)9>::callDerived<0ul>(swift::Evaluator&, std::__1::integer_sequence<unsigned long, 0ul>) const + 204
16 swift-frontend           0x0000000107a7461e swift::SimpleRequest<swift::IRGenRequest, swift::GeneratedModule (swift::IRGenDescriptor), (swift::RequestFlags)9>::evaluateRequest(swift::IRGenRequest const&, swift::Evaluator&) + 14
17 swift-frontend           0x0000000107a0cee3 llvm::Expected<swift::IRGenRequest::OutputType> swift::Evaluator::getResultUncached<swift::IRGenRequest>(swift::IRGenRequest const&) + 371
18 swift-frontend           0x0000000107a0040b swift::performIRGeneration(swift::FileUnit*, swift::IRGenOptions const&, swift::TBDGenOptions const&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>>, llvm::StringRef, swift::PrimarySpecificPaths const&, llvm::StringRef, llvm::GlobalVariable**) + 299
19 swift-frontend           0x00000001074f92c1 performCompileStepsPostSILGen(swift::CompilerInstance&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>>, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::PrimarySpecificPaths const&, int&, swift::FrontendObserver*) + 2065
20 swift-frontend           0x00000001074f82df swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 751
21 swift-frontend           0x00000001074fb756 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 950
22 swift-frontend           0x00000001074fa4a9 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3321
23 swift-frontend           0x0000000107494bea swift::mainEntry(int, char const**) + 2282
24 dyld                     0x000000012886e52e start + 462
Abort trap: 6

@max-christian
Copy link

Currently facing apparently the same issue on a huge codebase in Xcode 15.0 beta 2. There are a lot of source files in the arguments to swift-frontend. Is it safe to assume the problematic line of code is in the source file that follows the -primary-file option?

@AnthonyLatsis
Copy link
Collaborator

@max-christian Try compiling with a recent snapshot. The pretty stack trace should be more informative with recent revisions and specify both the file and source location like the one I posted above.

@max-christian
Copy link

Useful to know how to switch toolchains to the snapshot, thanks @AnthonyLatsis. Though unfortunately the compile phase is fine on the snapshot so I'm no closer to finding the culprit.

@insidegui
Copy link

@max-christian Here's how I described figuring out which file was causing the issue in a private thread, in case you want to try a similar approach:

Perhaps an unconventional approach, but I looked inside Intermediates.noindex/xxx.build/Debug/xxx.build/Objects-normal/arm64, sorted by size, and looked into the source files corresponding to the object files with zero bytes in length, assuming those were the files that likely triggered the crash. Commented out as much as possible from the affected files until the target built successfully. I've narrowed it down to a single file, will slowly reintroduce the code in that file until I can find which code exactly is causing the compiler to crash

@AnthonyLatsis
Copy link
Collaborator

Though unfortunately the compile phase is fine on the snapshot

Huh, what an elusive bug. Have you tried an older snapshot? One other option that might catch the broken invariant before stuff is handed over to LLVM is -sil-verify-all frontend flag.

@max-christian
Copy link

Huh, what an elusive bug. Have you tried an older snapshot? One other option that might catch the broken invariant before stuff is handed over to LLVM is -sil-verify-all frontend flag.

Perhaps not elusive but just a bug that's been fixed between Xcode 15.0 beta 2 and the snapshot I used? If there's a possibility it's a bug that needs attention I can open a separate issue, as the line of code that crashed the compiler isn't an exact match for this issue:

 ARScanViewController.queueModelUpdateJob { [n = liveSymbol?.node] in
                n?.removeFromParentNode()
  }

Thanks to @insidegui for the zero byte object file trick; that successfully narrowed it down to one source file.

@via-guy
Copy link

via-guy commented Jul 11, 2023

@max-christian Here's how I described figuring out which file was causing the issue in a private thread, in case you want to try a similar approach:

Perhaps an unconventional approach, but I looked inside Intermediates.noindex/xxx.build/Debug/xxx.build/Objects-normal/arm64, sorted by size, and looked into the source files corresponding to the object files with zero bytes in length, assuming those were the files that likely triggered the crash. Commented out as much as possible from the affected files until the target built successfully. I've narrowed it down to a single file, will slowly reintroduce the code in that file until I can find which code exactly is causing the compiler to crash

Thank you so much, I would have never discovered my issue without doing this! It helped me to discover that my code

var networkError: some Error {
   NSError(...)
}

was not triggering any compiler errors/warnings, but it was crashing the compiler. All I had to do was change it to any Error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
assertion failure Bug → crash: An assertion failure bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. CodeGen compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software generics Feature: generic declarations and types name shadowing Feature: name shadowing patterns Feature: patterns swift 5.5
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants