Skip to content

A concrete type parameter cannot be used as impl Trait if ? is used previously #93924

Closed
@lo48576

Description

@lo48576

Code

I tried this code:

use core::fmt::Debug;

#[derive(Debug)]
pub struct Target;

#[derive(Debug)]
pub struct Source;
impl From<Source> for Target {
    fn from(_: Source) -> Self {
        Self
    }
}

fn maybe_source() -> Result<(), Source> {
    todo!()
}

pub fn typaram() -> Result<(), impl Debug> {
    maybe_source()?;
    Ok::<_, Target>(())
}

pub fn direct() -> Result<(), impl Debug> {
    maybe_source()?;
    Err(Target)
}

I expected to see this happen: Compiles successfully, with Result<(), impl Debug> instantiated as Result<(), Target>.

Instead, this happened: Compilation error.

$ cargo +nightly --version --verbose
cargo 1.60.0-nightly (c082648 2022-02-08)
release: 1.60.0-nightly
commit-hash: c082648646cbb2be266df9ecbcdc253058158d68
commit-date: 2022-02-08
host: x86_64-unknown-linux-gnu
libgit2: 1.3.0 (sys:0.13.23 vendored)
libcurl: 7.80.0-DEV (sys:0.4.51+curl-7.80.0 vendored ssl:OpenSSL/1.1.1l)
os: Linux 2.8 [64-bit]
$ cargo +nightly check
    Checking impl-type-coercion v0.1.0 (/tmp/lo48576.ed4d0c1e-416f-417d-b689-0cfe81e092af/impl-type-coercion)
error[E0308]: mismatched types
  --> src/lib.rs:20:5
   |
18 | pub fn typaram() -> Result<(), impl Debug> {
   |                                ---------- the expected opaque type
19 |     maybe_source()?;
20 |     Ok::<_, Target>(())
   |     ^^^^^^^^^^^^^^^^^^^ expected struct `Source`, found struct `Target`
   |
   = note: expected opaque type `impl Debug`
                   found struct `Target`

error[E0308]: mismatched types
  --> src/lib.rs:25:5
   |
23 | pub fn direct() -> Result<(), impl Debug> {
   |                               ---------- the expected opaque type
24 |     maybe_source()?;
25 |     Err(Target)
   |     ^^^^^^^^^^^ expected struct `Source`, found struct `Target`
   |
   = note: expected opaque type `impl Debug`
                   found struct `Target`

For more information about this error, try `rustc --explain E0308`.
error: could not compile `impl-type-coercion` due to 2 previous errors
$

Without maybe_source()?; line, the errors don't happen.

Version it worked on

It most recently worked on:

  • rustc 1.58.1 (db9d1b2 2022-01-20) (current stable)
  • rustc 1.59.0-beta.6 (0426998 2022-02-02) (current beta)

Version with regression

rustc +nightly --version --verbose:

rustc 1.60.0-nightly (e646f3d2a 2022-02-10)
binary: rustc
commit-hash: e646f3d2a9541952310778288854943678738ea9
commit-date: 2022-02-10
host: x86_64-unknown-linux-gnu
release: 1.60.0-nightly
LLVM version: 13.0.0

@rustbot modify labels: +regression-from-stable-to-nightly -regression-untriaged

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.regression-from-stable-to-nightlyPerformance or correctness regression from stable to nightly.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions