Skip to content

Trait upcasting shadows (trait object) deref coercion #89190

Closed
@Tamschi

Description

@Tamschi

Code

I tried this code:

#![allow(dead_code)]

use core::ops::Deref;

trait A {}
trait B: A {}
impl<'a> Deref for dyn 'a + B {
    type Target = dyn A;
    fn deref(&self) -> &Self::Target {
        todo!()
    }
}

fn take_a(_: &dyn A) { }

fn whoops(b: &dyn B) {
    take_a(b) // <-- Regression here.
}

I expected to see this happen: This should probably continue to compile as before, unless proven to be equivalent.

Instead, this happened: The detection for #65991 seems to be a little too aggressive, so it's flagging the coercion as feature-gated in beta even though a deref coercion is available:

   Compiling playground v0.0.1 (/playground)
error[E0658]: trait upcasting coercion is experimental
  --> src/lib.rs:17:12
   |
17 |     take_a(b)
   |            ^
   |
   = note: see issue #65991 <https://github.com/rust-lang/rust/issues/65991> for more information

For more information about this error, try `rustc --explain E0658`.
error: could not compile `playground` due to previous error

Playground: https://play.rust-lang.org/?version=beta&mode=debug&edition=2018&gist=0d7f48d15e1aa249d953fa7d5b6cc2f9

Version it worked on

It most recently worked on: 1.55.0

rustc +stable --version --verbose:

rustc 1.55.0 (c8dfcfe04 2021-09-06)
binary: rustc
commit-hash: c8dfcfe046a7680554bf4eb612bad840e7631c4b
commit-date: 2021-09-06
host: x86_64-unknown-linux-gnu
release: 1.55.0
LLVM version: 12.0.1

Version with regression

rustc +beta --version --verbose:

rustc 1.56.0-beta.3 (deef86610 2021-09-17)
binary: rustc
commit-hash: deef866109022248b2506587b377c6b1c881d963
commit-date: 2021-09-17
host: x86_64-unknown-linux-gnu
release: 1.56.0-beta.3
LLVM version: 13.0.0

Backtrace

(not applicable)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-trait-systemArea: Trait systemC-bugCategory: This is a bug.F-trait_upcasting`#![feature(trait_upcasting)]`P-highHigh priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language teamregression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions