Skip to content

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

Closed
@lo48576

Description

@lo48576
Contributor

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

Activity

added
C-bugCategory: This is a bug.
regression-untriagedUntriaged performance or correctness regression.
on Feb 11, 2022
added
I-prioritizeIssue: Indicates that prioritization has been requested for this issue.
and removed
regression-untriagedUntriaged performance or correctness regression.
on Feb 11, 2022
lo48576

lo48576 commented on Feb 11, 2022

@lo48576
ContributorAuthor

Doctests that return Result<_, _> might be affected.

use core::fmt::Debug;

#[derive(Debug)]
pub struct Target;

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

/// # Examples
///
/// ```
/// use impl_type_coercion::{Target, maybe_source};
/// maybe_source()?;
/// Ok::<_, Target>(())
/// ```
pub fn maybe_source() -> Result<(), Source> {
    todo!()
}
Output of cargo +nightly test:
$ cargo +nightly test
    Finished test [unoptimized + debuginfo] target(s) in 0.00s
     Running unittests (target/debug/deps/impl_type_coercion-5c03dc1e98d7ad90)

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

   Doc-tests impl-type-coercion

running 1 test
test src/lib.rs - maybe_source (line 16) ... FAILED

failures:

---- src/lib.rs - maybe_source (line 16) stdout ----
error[E0308]: mismatched types
 --> src/lib.rs:19:1
  |
3 | fn main() { #[allow(non_snake_case)] fn _doctest_main_src_lib_rs_16_0() -> Result<(), impl core::fmt::Debug> {
  |                                                                                       --------------------- the expected opaque type
...
6 | Ok::<_, Target>(())
  | ^^^^^^^^^^^^^^^^^^^ expected struct `Source`, found struct `Target`
  |
  = note: expected opaque type `impl Debug`
                  found struct `Target`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.
Couldn't compile the test.

failures:
    src/lib.rs - maybe_source (line 16)

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.03s

error: test failed, to rerun pass '--doc'
$
lo48576

lo48576 commented on Feb 12, 2022

@lo48576
ContributorAuthor

searched nightlies: from nightly-2022-02-01 to nightly-2022-02-11
regressed nightly: nightly-2022-02-09
searched commit range: 734368a...0c292c9
regressed commit: e7cc3bd

bisected with cargo-bisect-rustc v0.6.1

Host triple: x86_64-unknown-linux-gnu
Reproduce with:

cargo bisect-rustc --start=2022-02-01 --end=2022-02-11
compiler-errors

compiler-errors commented on Feb 12, 2022

@compiler-errors
Member

This got reverted in #93893

lo48576

lo48576 commented on Feb 12, 2022

@lo48576
ContributorAuthor

Thank you!
Closing this issue as I confirmed this is fixed at rustc 1.60.0-nightly (e789f3a3a 2022-02-11).

removed
I-prioritizeIssue: Indicates that prioritization has been requested for this issue.
on Feb 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

        Participants

        @lo48576@compiler-errors@apiraino@rustbot

        Issue actions

          A concrete type parameter cannot be used as `impl Trait` if `?` is used previously · Issue #93924 · rust-lang/rust