Skip to content

Commit 5d9b514

Browse files
committed
Do not silence some sized errors as they would cause ICEs
1 parent f617130 commit 5d9b514

File tree

5 files changed

+35
-22
lines changed

5 files changed

+35
-22
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -641,30 +641,21 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
641641

642642
if Some(trait_ref.def_id()) == self.tcx.lang_items().sized_trait() {
643643
if !self.tcx.object_safety_violations(trait_ref.def_id()).is_empty() {
644-
match (
645-
obligation.cause.code(),
646-
trait_ref.skip_binder().self_ty().kind(),
647-
) {
648-
(
649-
ObligationCauseCode::WhereClauseInExpr(..),
650-
ty::Dynamic(..),
651-
) => {
644+
if let ObligationCauseCode::SizedCallReturnType(did)
645+
= obligation.cause.code()
646+
{
647+
let fn_sig = self.tcx.fn_sig(did);
648+
let ret_kind =
649+
fn_sig.skip_binder().output().skip_binder().kind();
650+
if let ty::Param(param_ty) = ret_kind
651+
&& param_ty.name == kw::SelfUpper
652+
{
652653
return err.delay_as_bug();
653654
}
654-
(ObligationCauseCode::SizedCallReturnType(did), _) => {
655-
let fn_sig = self.tcx.fn_sig(did);
656-
let ret_kind =
657-
fn_sig.skip_binder().output().skip_binder().kind();
658-
if let ty::Param(param_ty) = ret_kind
659-
&& param_ty.name == kw::SelfUpper
660-
{
661-
return err.delay_as_bug();
662-
}
663-
}
664-
_ => {}
665655
}
666656
}
667657
}
658+
668659
if let ObligationCauseCode::Coercion { source, target } =
669660
*obligation.cause.code().peel_derives()
670661
{

tests/ui/dyn-keyword/trait-missing-dyn-in-qualified-path.edition2018.stderr

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ LL | let x: u32 = <Default>::default();
2121
= note: the trait cannot be made into an object because it requires `Self: Sized`
2222
= 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>
2323

24+
error[E0277]: the size for values of type `dyn Default` cannot be known at compilation time
25+
--> $DIR/trait-missing-dyn-in-qualified-path.rs:5:19
26+
|
27+
LL | let x: u32 = <Default>::default();
28+
| ^^^^^^^ doesn't have a size known at compile-time
29+
|
30+
= help: the trait `Sized` is not implemented for `dyn Default`
31+
note: required by a bound in `default`
32+
--> $SRC_DIR/core/src/default.rs:LL:COL
33+
2434
error[E0308]: mismatched types
2535
--> $DIR/trait-missing-dyn-in-qualified-path.rs:5:18
2636
|
@@ -42,7 +52,7 @@ LL | let x: u32 = <Default>::default();
4252
= note: the trait cannot be made into an object because it requires `Self: Sized`
4353
= 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>
4454

45-
error: aborting due to 3 previous errors; 1 warning emitted
55+
error: aborting due to 4 previous errors; 1 warning emitted
4656

47-
Some errors have detailed explanations: E0038, E0308.
57+
Some errors have detailed explanations: E0038, E0277, E0308.
4858
For more information about an error, try `rustc --explain E0038`.

tests/ui/dyn-keyword/trait-missing-dyn-in-qualified-path.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ fn main() {
99
//[edition2018]~| ERROR trait `Default` cannot be made into an object
1010
//[edition2018]~| ERROR trait `Default` cannot be made into an object
1111
//[edition2018]~| ERROR mismatched types
12+
//[edition2018]~| ERROR the size for values of type `dyn Default` cannot be known at compilation time
1213
}

tests/ui/traits/bad-sized.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ pub fn main() {
55
//~^ ERROR only auto traits can be used as additional traits in a trait object
66
//~| ERROR the size for values of type
77
//~| ERROR the size for values of type
8+
//~| ERROR the size for values of type
89
}

tests/ui/traits/bad-sized.stderr

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ LL | let x: Vec<dyn Trait + Sized> = Vec::new();
1919
note: required by an implicit `Sized` bound in `Vec`
2020
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
2121

22+
error[E0277]: the size for values of type `dyn Trait` cannot be known at compilation time
23+
--> $DIR/bad-sized.rs:4:37
24+
|
25+
LL | let x: Vec<dyn Trait + Sized> = Vec::new();
26+
| ^^^^^^^^^^ doesn't have a size known at compile-time
27+
|
28+
= help: the trait `Sized` is not implemented for `dyn Trait`
29+
note: required by a bound in `Vec::<T>::new`
30+
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
31+
2232
error[E0277]: the size for values of type `dyn Trait` cannot be known at compilation time
2333
--> $DIR/bad-sized.rs:4:37
2434
|
@@ -29,7 +39,7 @@ LL | let x: Vec<dyn Trait + Sized> = Vec::new();
2939
note: required by an implicit `Sized` bound in `Vec`
3040
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
3141

32-
error: aborting due to 3 previous errors
42+
error: aborting due to 4 previous errors
3343

3444
Some errors have detailed explanations: E0225, E0277.
3545
For more information about an error, try `rustc --explain E0225`.

0 commit comments

Comments
 (0)