Skip to content

bare_trait_objects help is incorrect with Box<Trait + 'lifetime> in 2015 edition #63330

Open
@mmitteregger

Description

@mmitteregger

The help text message for bare_trait_objects is incorrect when using the 2015 edition.

The following example gives a correct help text in the 2015 and 2018 edition:

pub fn test(_: Box<::std::any::Any>) {}
warning: trait objects without an explicit `dyn` are deprecated
 --> src/lib.rs:1:20
  |
1 | pub fn test(_: Box<::std::any::Any>) {
  |                    ^^^^^^^^^^^^^^^ help: use `dyn`: `dyn (::std::any::Any)`
  |
  = note: `#[warn(bare_trait_objects)]` on by default

but adding a lifetime to the trait bound produces an incorrect help message for the 2015 edition:

pub fn test(_: Box<::std::any::Any + 'static>) {
}
warning: trait objects without an explicit `dyn` are deprecated
 --> src/lib.rs:1:20
  |
1 | pub fn test(_: Box<::std::any::Any + 'static>) {
  |                    ^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `dyn`: `dyn ::std::any::Any + 'static`
  |
  = note: `#[warn(bare_trait_objects)]` on by default

Applying the suggest help text in 2015 edition code results in:

pub fn test(_: Box<dyn ::std::any::Any + 'static>) {
}
error[E0433]: failed to resolve: use of undeclared type or module `dyn`
 --> src/lib.rs:1:20
  |
1 | pub fn test(_: Box<dyn ::std::any::Any + 'static>) {
  |                    ^^^ use of undeclared type or module `dyn`

warning: trait objects without an explicit `dyn` are deprecated
 --> src/lib.rs:1:20
  |
1 | pub fn test(_: Box<dyn ::std::any::Any + 'static>) {
  |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `dyn`: `dyn dyn ::std::any::Any + 'static`
  |
  = note: `#[warn(bare_trait_objects)]` on by default

error: aborting due to previous error

For more information about this error, try `rustc --explain E0433`.
error: Could not compile `playground`.

To learn more, run the command again with --verbose.

The code actually compiles with the 2018 edition.
The correct fix for 2015 edition code (which also works for edition 2018) is probably to include parens:

pub fn test(_: Box<dyn (::std::any::Any) + 'static>) {
}

I see two options:

  1. Add parens to the help text so that it is correct in both editions.
  2. Fix the help text only for the 2015 edition, because the suggested fix is already correct for the 2018 edition.

I personally prefer the first option for consistency with the existing help text and easier copy/pasting between editions.

Meta

rustc --version --verbose:

rustc 1.38.0-nightly (c4715198b 2019-08-05)
binary: rustc
commit-hash: c4715198b50d1cdaad44b6e250844362b77dcdd7
commit-date: 2019-08-05
host: x86_64-pc-windows-gnu
release: 1.38.0-nightly
LLVM version: 9.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.D-editionDiagnostics: An error or lint that should account for edition differences.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions