Open JadenGeller opened 2 months ago
I updated the expected behavior from
Cannot convert value of type 'String' to specified type 'Int'
to
Cannot convert value of type 'String' to specified type 'some ExampleProtocol' (result of 'ExampleFactory.makeExample()')
since I don't think type inference should act as a side-channel to determine the concrete instance of an opaque type.
A simpler example:
// test.swift
func f() -> some Sequence { "" }
typealias A = @_opaqueReturnTypeOf("$s4test1fQryF", 0) __
let a: A = "" // OK
let b: A.Element = "" // Crash
let c: A
c = "" // error: cannot assign value of type 'String' to type 'A' (aka 'some Sequence') [cannot_convert_assign]
I suspect we are wrongly assuming somewhere that an explicit type annotation necessarily implies the opaque types it represents are declared on the variable.
Description / Reproduction
The following ought to generate a type-error where
whoops
is declared, but instead it complies and violates type safety, interpreting memory as incorrect type.In the example above, the
String
"hi"
was reinterpreted as anInt
sinceExampleFactor.Example
isInt
.If we were to
unsafeBitCast
aString
toInt
, it would trap since the sizes don't match. Instead, we can run the following:As you can see, the first component matches the value of
whoops
, confirming that Swift in the first example silently reinterpretedwhoops
's initializing value as a conflicting type.Expected behavior
Environment
swift-driver version: 1.87.3 Apple Swift version 5.9.2 (swiftlang-5.9.2.2.56 clang-1500.1.0.2.5) Target: arm64-apple-macosx14.0
Additional information
No response