-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Avoid ICE in trait without dyn
lint
#120275
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
error[E0038]: the trait `Copy` cannot be made into an object | ||
--> $DIR/avoid-ice-on-warning-2.rs:4:13 | ||
| | ||
LL | fn id<F>(f: Copy) -> usize { | ||
| ^^^^ `Copy` cannot be made into an object | ||
| | ||
= note: the trait cannot be made into an object because it requires `Self: Sized` | ||
= note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety> | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0038`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,33 @@ | ||
warning: trait objects without an explicit `dyn` are deprecated | ||
--> $DIR/avoid-ice-on-warning-2.rs:1:13 | ||
--> $DIR/avoid-ice-on-warning-2.rs:4:13 | ||
| | ||
LL | fn id<F>(f: Copy) -> usize { | ||
| ^^^^ | ||
| | ||
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! | ||
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html> | ||
= note: `Copy` it is not object safe, so it can't be `dyn` | ||
= note: `#[warn(bare_trait_objects)]` on by default | ||
help: use a new generic type parameter, constrained by `Copy` | ||
help: use `dyn` | ||
| | ||
LL | fn id<F, T: Copy>(f: T) -> usize { | ||
| +++++++++ ~ | ||
help: you can also use an opaque type, but users won't be able to specify the type parameter when calling the `fn`, having to rely exclusively on type inference | ||
| | ||
LL | fn id<F>(f: impl Copy) -> usize { | ||
| ++++ | ||
LL | fn id<F>(f: dyn Copy) -> usize { | ||
| +++ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is adding an invalid suggestion to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, that's expected. Note that this only happens in Rust <2021. From the PR description:
|
||
|
||
warning: trait objects without an explicit `dyn` are deprecated | ||
--> $DIR/avoid-ice-on-warning-2.rs:1:13 | ||
--> $DIR/avoid-ice-on-warning-2.rs:4:13 | ||
| | ||
LL | fn id<F>(f: Copy) -> usize { | ||
| ^^^^ | ||
| | ||
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! | ||
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html> | ||
= note: `Copy` it is not object safe, so it can't be `dyn` | ||
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` | ||
help: use a new generic type parameter, constrained by `Copy` | ||
| | ||
LL | fn id<F, T: Copy>(f: T) -> usize { | ||
| +++++++++ ~ | ||
help: you can also use an opaque type, but users won't be able to specify the type parameter when calling the `fn`, having to rely exclusively on type inference | ||
help: use `dyn` | ||
| | ||
LL | fn id<F>(f: impl Copy) -> usize { | ||
| ++++ | ||
LL | fn id<F>(f: dyn Copy) -> usize { | ||
| +++ | ||
|
||
error[E0038]: the trait `Copy` cannot be made into an object | ||
--> $DIR/avoid-ice-on-warning-2.rs:1:13 | ||
--> $DIR/avoid-ice-on-warning-2.rs:4:13 | ||
| | ||
LL | fn id<F>(f: Copy) -> usize { | ||
| ^^^^ `Copy` cannot be made into an object | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
// revisions: old new | ||
//[old] edition:2015 | ||
//[new] edition:2021 | ||
fn id<F>(f: Copy) -> usize { | ||
//~^ WARN trait objects without an explicit `dyn` are deprecated | ||
//~| WARN this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! | ||
//~| WARN trait objects without an explicit `dyn` are deprecated | ||
//~| WARN this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! | ||
//~| ERROR the trait `Copy` cannot be made into an object | ||
//~^ ERROR the trait `Copy` cannot be made into an object | ||
//[old]~| WARN trait objects without an explicit `dyn` are deprecated | ||
//[old]~| WARN this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! | ||
//[old]~| WARN trait objects without an explicit `dyn` are deprecated | ||
//[old]~| WARN this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! | ||
f() | ||
} | ||
fn main() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
error[E0038]: the trait `A` cannot be made into an object | ||
--> $DIR/avoid-ice-on-warning-3.rs:4:19 | ||
| | ||
LL | trait B { fn f(a: A) -> A; } | ||
| ^ `A` cannot be made into an object | ||
| | ||
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety> | ||
--> $DIR/avoid-ice-on-warning-3.rs:12:14 | ||
| | ||
LL | trait A { fn g(b: B) -> B; } | ||
| - ^ ...because associated function `g` has no `self` parameter | ||
| | | ||
| this trait cannot be made into an object... | ||
help: consider turning `g` into a method by giving it a `&self` argument | ||
| | ||
LL | trait A { fn g(&self, b: B) -> B; } | ||
| ++++++ | ||
help: alternatively, consider constraining `g` so it does not apply to trait objects | ||
| | ||
LL | trait A { fn g(b: B) -> B where Self: Sized; } | ||
| +++++++++++++++++ | ||
|
||
error[E0038]: the trait `B` cannot be made into an object | ||
--> $DIR/avoid-ice-on-warning-3.rs:12:19 | ||
| | ||
LL | trait A { fn g(b: B) -> B; } | ||
| ^ `B` cannot be made into an object | ||
| | ||
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety> | ||
--> $DIR/avoid-ice-on-warning-3.rs:4:14 | ||
| | ||
LL | trait B { fn f(a: A) -> A; } | ||
| - ^ ...because associated function `f` has no `self` parameter | ||
| | | ||
| this trait cannot be made into an object... | ||
help: consider turning `f` into a method by giving it a `&self` argument | ||
| | ||
LL | trait B { fn f(&self, a: A) -> A; } | ||
| ++++++ | ||
help: alternatively, consider constraining `f` so it does not apply to trait objects | ||
| | ||
LL | trait B { fn f(a: A) -> A where Self: Sized; } | ||
| +++++++++++++++++ | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0038`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,20 @@ | ||
// revisions: old new | ||
//[old] edition:2015 | ||
//[new] edition:2021 | ||
trait B { fn f(a: A) -> A; } | ||
//~^ WARN trait objects without an explicit `dyn` are deprecated | ||
//~| WARN trait objects without an explicit `dyn` are deprecated | ||
//~| WARN trait objects without an explicit `dyn` are deprecated | ||
//~| WARN this is accepted in the current edition | ||
//~| WARN this is accepted in the current edition | ||
//~| WARN this is accepted in the current edition | ||
//~| ERROR the trait `A` cannot be made into an object | ||
//~^ ERROR the trait `A` cannot be made into an object | ||
//[old]~| WARN trait objects without an explicit `dyn` are deprecated | ||
//[old]~| WARN trait objects without an explicit `dyn` are deprecated | ||
//[old]~| WARN trait objects without an explicit `dyn` are deprecated | ||
//[old]~| WARN this is accepted in the current edition | ||
//[old]~| WARN this is accepted in the current edition | ||
//[old]~| WARN this is accepted in the current edition | ||
trait A { fn g(b: B) -> B; } | ||
//~^ WARN trait objects without an explicit `dyn` are deprecated | ||
//~| WARN trait objects without an explicit `dyn` are deprecated | ||
//~| WARN trait objects without an explicit `dyn` are deprecated | ||
//~| WARN this is accepted in the current edition | ||
//~| WARN this is accepted in the current edition | ||
//~| WARN this is accepted in the current edition | ||
//~| ERROR the trait `B` cannot be made into an object | ||
//~^ ERROR the trait `B` cannot be made into an object | ||
//[old]~| WARN trait objects without an explicit `dyn` are deprecated | ||
//[old]~| WARN trait objects without an explicit `dyn` are deprecated | ||
//[old]~| WARN trait objects without an explicit `dyn` are deprecated | ||
//[old]~| WARN this is accepted in the current edition | ||
//[old]~| WARN this is accepted in the current edition | ||
//[old]~| WARN this is accepted in the current edition | ||
fn main() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
error: return types are denoted using `->` | ||
--> $DIR/avoid-ice-on-warning.rs:4:23 | ||
| | ||
LL | fn call_this<F>(f: F) : Fn(&str) + call_that {} | ||
| ^ help: use `->` instead | ||
|
||
error[E0405]: cannot find trait `call_that` in this scope | ||
--> $DIR/avoid-ice-on-warning.rs:4:36 | ||
| | ||
LL | fn call_this<F>(f: F) : Fn(&str) + call_that {} | ||
| ^^^^^^^^^ not found in this scope | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0405`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now we should be able to remove the
diag.is_error()
insidemaybe_lint_impl_trait
again simplifying the latter.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't bother removing them earlier because it didn't affect the line count at all (we already have to check for
is_downgradable
anyways. Regardless, done.