forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#127692 - veera-sivarajan:bugfix-125139, r=estebank Suggest `impl Trait` for References to Bare Trait in Function Header Fixes rust-lang#125139 This PR suggests `impl Trait` when `&Trait` is found as a function parameter type or return type. This makes use of existing diagnostics by adding `peel_refs()` when checking for type equality. Additionaly, it makes a few other improvements: 1. Checks if functions inside impl blocks have bare trait in their headers. 2. Introduces a trait `NextLifetimeParamName` similar to the existing `NextTypeParamName` for suggesting a lifetime name. Also, abstracts out the common logic between the two trait impls. ### Related Issues I ran into a bunch of related diagnostic issues but couldn't fix them within the scope of this PR. So, I have created the following issues: 1. [Misleading Suggestion when Returning a Reference to a Bare Trait from a Function](rust-lang#127689) 2. [Verbose Error When a Function Takes a Bare Trait as Parameter](rust-lang#127690) 3. [Incorrect Suggestion when Returning a Bare Trait from a Function](rust-lang#127691) r? ```@estebank``` since you implemented rust-lang#119148
- Loading branch information
Showing
5 changed files
with
891 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
tests/ui/object-safety/reference-to-bare-trait-in-fn-inputs-and-outputs-issue-125139.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
//@ edition:2021 | ||
|
||
trait Trait {} | ||
|
||
struct IceCream; | ||
|
||
impl IceCream { | ||
fn foo(_: &Trait) {} | ||
//~^ ERROR: trait objects must include the `dyn` keyword | ||
|
||
fn bar(self, _: &'a Trait) {} | ||
//~^ ERROR: trait objects must include the `dyn` keyword | ||
//~| ERROR: use of undeclared lifetime name | ||
|
||
fn alice<'a>(&self, _: &Trait) {} | ||
//~^ ERROR: trait objects must include the `dyn` keyword | ||
|
||
fn bob<'a>(_: &'a Trait) {} | ||
//~^ ERROR: trait objects must include the `dyn` keyword | ||
|
||
fn cat() -> &Trait { | ||
//~^ ERROR: missing lifetime specifier | ||
//~| ERROR: trait objects must include the `dyn` keyword | ||
&Type | ||
} | ||
|
||
fn dog<'a>() -> &Trait { | ||
//~^ ERROR: missing lifetime specifier | ||
//~| ERROR: trait objects must include the `dyn` keyword | ||
&Type | ||
} | ||
|
||
fn kitten() -> &'a Trait { | ||
//~^ ERROR: use of undeclared lifetime name | ||
//~| ERROR: trait objects must include the `dyn` keyword | ||
&Type | ||
} | ||
|
||
fn puppy<'a>() -> &'a Trait { | ||
//~^ ERROR: trait objects must include the `dyn` keyword | ||
&Type | ||
} | ||
|
||
fn parrot() -> &mut Trait { | ||
//~^ ERROR: missing lifetime specifier | ||
//~| ERROR: trait objects must include the `dyn` keyword | ||
&mut Type | ||
//~^ ERROR: cannot return reference to temporary value | ||
} | ||
} | ||
|
||
trait Sing { | ||
fn foo(_: &Trait); | ||
//~^ ERROR: trait objects must include the `dyn` keyword | ||
|
||
fn bar(_: &'a Trait); | ||
//~^ ERROR: trait objects must include the `dyn` keyword | ||
//~| ERROR: use of undeclared lifetime name | ||
|
||
fn alice<'a>(_: &Trait); | ||
//~^ ERROR: trait objects must include the `dyn` keyword | ||
|
||
fn bob<'a>(_: &'a Trait); | ||
//~^ ERROR: trait objects must include the `dyn` keyword | ||
|
||
fn cat() -> &Trait; | ||
//~^ ERROR: missing lifetime specifier | ||
//~| ERROR: trait objects must include the `dyn` keyword | ||
|
||
fn dog<'a>() -> &Trait { | ||
//~^ ERROR: missing lifetime specifier | ||
//~| ERROR: trait objects must include the `dyn` keyword | ||
&Type | ||
} | ||
|
||
fn kitten() -> &'a Trait { | ||
//~^ ERROR: use of undeclared lifetime name | ||
//~| ERROR: trait objects must include the `dyn` keyword | ||
&Type | ||
} | ||
|
||
fn puppy<'a>() -> &'a Trait { | ||
//~^ ERROR: trait objects must include the `dyn` keyword | ||
&Type | ||
} | ||
|
||
fn parrot() -> &mut Trait { | ||
//~^ ERROR: missing lifetime specifier | ||
//~| ERROR: trait objects must include the `dyn` keyword | ||
&mut Type | ||
//~^ ERROR: cannot return reference to temporary value | ||
} | ||
} | ||
|
||
fn foo(_: &Trait) {} | ||
//~^ ERROR: trait objects must include the `dyn` keyword | ||
|
||
fn bar(_: &'a Trait) {} | ||
//~^ ERROR: trait objects must include the `dyn` keyword | ||
//~| ERROR: use of undeclared lifetime name | ||
|
||
fn alice<'a>(_: &Trait) {} | ||
//~^ ERROR: trait objects must include the `dyn` keyword | ||
|
||
fn bob<'a>(_: &'a Trait) {} | ||
//~^ ERROR: trait objects must include the `dyn` keyword | ||
|
||
struct Type; | ||
|
||
impl Trait for Type {} | ||
|
||
fn cat() -> &Trait { | ||
//~^ ERROR: missing lifetime specifier | ||
//~| ERROR: trait objects must include the `dyn` keyword | ||
&Type | ||
} | ||
|
||
fn dog<'a>() -> &Trait { | ||
//~^ ERROR: missing lifetime specifier | ||
//~| ERROR: trait objects must include the `dyn` keyword | ||
&Type | ||
} | ||
|
||
fn kitten() -> &'a Trait { | ||
//~^ ERROR: use of undeclared lifetime name | ||
//~| ERROR: trait objects must include the `dyn` keyword | ||
&Type | ||
} | ||
|
||
fn puppy<'a>() -> &'a Trait { | ||
//~^ ERROR: trait objects must include the `dyn` keyword | ||
&Type | ||
} | ||
|
||
fn parrot() -> &mut Trait { | ||
//~^ ERROR: missing lifetime specifier | ||
//~| ERROR: trait objects must include the `dyn` keyword | ||
&mut Type | ||
//~^ ERROR: cannot return reference to temporary value | ||
} | ||
|
||
fn main() {} |
Oops, something went wrong.