Skip to content

Commit

Permalink
Rollup merge of rust-lang#120275 - estebank:issue-120241, r=fmease
Browse files Browse the repository at this point in the history
Avoid ICE in trait without `dyn` lint

Do not attempt to provide an accurate suggestion for `impl Trait` in bare trait types when linting. Instead, only do the object safety check when an E0782 is already going to be emitted in the 2021 edition.

Fix rust-lang#120241.
  • Loading branch information
Nadrieril authored Jan 27, 2024
2 parents aff6961 + 3022c76 commit 4ada5ef
Show file tree
Hide file tree
Showing 16 changed files with 222 additions and 126 deletions.
10 changes: 4 additions & 6 deletions compiler/rustc_hir_analysis/src/astconv/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
],
Applicability::MachineApplicable,
);
} else if diag.is_error() && is_downgradable {
} else if is_downgradable {
// We'll emit the object safety error already, with a structured suggestion.
diag.downgrade_to_delayed_bug();
}
Expand All @@ -158,7 +158,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
}
if !is_object_safe {
diag.note(format!("`{trait_name}` it is not object safe, so it can't be `dyn`"));
if diag.is_error() && is_downgradable {
if is_downgradable {
// We'll emit the object safety error already, with a structured suggestion.
diag.downgrade_to_delayed_bug();
}
Expand Down Expand Up @@ -241,13 +241,11 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
} else {
let msg = "trait objects without an explicit `dyn` are deprecated";
tcx.node_span_lint(BARE_TRAIT_OBJECTS, self_ty.hir_id, self_ty.span, msg, |lint| {
if self_ty.span.can_be_used_for_suggestions()
&& !self.maybe_lint_impl_trait(self_ty, lint)
{
if self_ty.span.can_be_used_for_suggestions() {
lint.multipart_suggestion_verbose(
"use `dyn`",
sugg,
Applicability::MachineApplicable,
Applicability::MaybeIncorrect,
);
}
self.maybe_lint_blanket_trait_impl(self_ty, lint);
Expand Down
12 changes: 12 additions & 0 deletions tests/ui/object-safety/avoid-ice-on-warning-2.new.stderr
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 {
| +++

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
Expand Down
13 changes: 8 additions & 5 deletions tests/ui/object-safety/avoid-ice-on-warning-2.rs
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() {}
47 changes: 47 additions & 0 deletions tests/ui/object-safety/avoid-ice-on-warning-3.new.stderr
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,93 +1,78 @@
warning: trait objects without an explicit `dyn` are deprecated
--> $DIR/avoid-ice-on-warning-3.rs:9:19
--> $DIR/avoid-ice-on-warning-3.rs:4:19
|
LL | trait A { fn g(b: B) -> B; }
LL | trait B { fn f(a: A) -> A; }
| ^
|
= 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: `B` 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 `B`
|
LL | trait A { fn g<T: B>(b: T) -> B; }
| ++++++ ~
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 | trait A { fn g(b: impl B) -> B; }
| ++++
LL | trait B { fn f(a: dyn A) -> A; }
| +++

warning: trait objects without an explicit `dyn` are deprecated
--> $DIR/avoid-ice-on-warning-3.rs:9:25
--> $DIR/avoid-ice-on-warning-3.rs:4:25
|
LL | trait A { fn g(b: B) -> B; }
LL | trait B { fn f(a: A) -> A; }
| ^
|
= 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>
help: `B` is not object safe, use `impl B` to return an opaque type, as long as you return a single underlying type
help: use `dyn`
|
LL | trait A { fn g(b: B) -> impl B; }
| ++++
LL | trait B { fn f(a: A) -> dyn A; }
| +++

warning: trait objects without an explicit `dyn` are deprecated
--> $DIR/avoid-ice-on-warning-3.rs:1:19
--> $DIR/avoid-ice-on-warning-3.rs:12:19
|
LL | trait B { fn f(a: A) -> A; }
LL | trait A { fn g(b: B) -> B; }
| ^
|
= 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: `A` it is not object safe, so it can't be `dyn`
help: use a new generic type parameter, constrained by `A`
|
LL | trait B { fn f<T: A>(a: T) -> A; }
| ++++++ ~
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 | trait B { fn f(a: impl A) -> A; }
| ++++
LL | trait A { fn g(b: dyn B) -> B; }
| +++

warning: trait objects without an explicit `dyn` are deprecated
--> $DIR/avoid-ice-on-warning-3.rs:1:25
--> $DIR/avoid-ice-on-warning-3.rs:12:25
|
LL | trait B { fn f(a: A) -> A; }
LL | trait A { fn g(b: B) -> B; }
| ^
|
= 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>
help: `A` is not object safe, use `impl A` to return an opaque type, as long as you return a single underlying type
help: use `dyn`
|
LL | trait B { fn f(a: A) -> impl A; }
| ++++
LL | trait A { fn g(b: B) -> dyn B; }
| +++

warning: trait objects without an explicit `dyn` are deprecated
--> $DIR/avoid-ice-on-warning-3.rs:1:19
--> $DIR/avoid-ice-on-warning-3.rs:4:19
|
LL | trait B { fn f(a: A) -> A; }
| ^
|
= 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: `A` 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 `A`
|
LL | trait B { fn f<T: A>(a: T) -> A; }
| ++++++ ~
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 | trait B { fn f(a: impl A) -> A; }
| ++++
LL | trait B { fn f(a: dyn A) -> A; }
| +++

error[E0038]: the trait `A` cannot be made into an object
--> $DIR/avoid-ice-on-warning-3.rs:1:19
--> $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:9:14
--> $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
Expand All @@ -103,32 +88,27 @@ LL | trait A { fn g(b: B) -> B where Self: Sized; }
| +++++++++++++++++

warning: trait objects without an explicit `dyn` are deprecated
--> $DIR/avoid-ice-on-warning-3.rs:9:19
--> $DIR/avoid-ice-on-warning-3.rs:12:19
|
LL | trait A { fn g(b: B) -> B; }
| ^
|
= 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: `B` 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 `B`
|
LL | trait A { fn g<T: B>(b: T) -> B; }
| ++++++ ~
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 | trait A { fn g(b: impl B) -> B; }
| ++++
LL | trait A { fn g(b: dyn B) -> B; }
| +++

error[E0038]: the trait `B` cannot be made into an object
--> $DIR/avoid-ice-on-warning-3.rs:9:19
--> $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:1:14
--> $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
Expand Down
31 changes: 17 additions & 14 deletions tests/ui/object-safety/avoid-ice-on-warning-3.rs
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() {}
15 changes: 15 additions & 0 deletions tests/ui/object-safety/avoid-ice-on-warning.new.stderr
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`.
Loading

0 comments on commit 4ada5ef

Please sign in to comment.