swiftlang / swift

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

Failed to produce diagnostic with optionals and generics (minimal repro) #64238

Open tristanlabelle opened 1 year ago

tristanlabelle commented 1 year ago

Description The Swift compiler produces a fatal error when compiling a combination of generics and optionals.

Steps to reproduce Compile the following with swiftc:

class Repro {
    func foo(repro: Repro?) { _ = repro?.bar() }
    func bar<T>() -> T { fatalError("") }
}

Output:

Building for debugging...
C:\Code\browser-swift\Apps\BrowserWin\Source\Application\ArcApplication.swift:2:33: error: failed to produce diagnostic for expression; please submit a bug report (https://swift.org/contributing/#reporting-bugs)
    func foo(repro: Repro?) { _ = repro?.bar() }
                                ^
error: fatalError

Expected behavior A diagnostic should be produced and the compiler should not fatalError

Environment

xedin commented 1 year ago

Looks like we binding type variable allocated for optional evaluation expression to the one that represents generic parameter and that ends up propagating "can be hole" trait to the former one without a fix.

xedin commented 1 year ago

cc @LucianoPAlmeida maybe interesting, I wonder if we need to change constraint generation for optional evaluation expression slightly to avoid this kind of problem.

LucianoPAlmeida commented 1 year ago

I remember this one, is the propagation logic we have for optional evaluation right? Was taking a look at the comment and it was mainly for pattern context, but now that pattern variables are marked as holes this propagation is still needed? Maybe I am missing something or it still needed in general. What is the idea on adjusting CSGen logic?