Skip to content

Commit 936b692

Browse files
committed
Silence unsized types errors in some expressions
When encountering an unsized E0277 error on a method that returns `Self` silence that error as it a prior unsized error will have been emitted when constructing the value of `Self`. On E0227 unsized error caused on a bare trait associated function with object safety violations, silence it.
1 parent 26cfeb1 commit 936b692

File tree

8 files changed

+33
-39
lines changed

8 files changed

+33
-39
lines changed

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
591591
self.require_type_is_sized_deferred(
592592
output,
593593
call.map_or(expr.span, |e| e.span),
594-
ObligationCauseCode::SizedCallReturnType,
594+
ObligationCauseCode::SizedCallReturnType(did),
595595
);
596596
}
597597

compiler/rustc_middle/src/traits/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ pub enum ObligationCauseCode<'tcx> {
284284
/// Return type must be `Sized`.
285285
SizedReturnType,
286286
/// Return type of a call expression must be `Sized`.
287-
SizedCallReturnType,
287+
SizedCallReturnType(DefId),
288288
/// Yield type must be `Sized`.
289289
SizedYieldType,
290290
/// Inline asm operand type must be `Sized`.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3081,7 +3081,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
30813081
err.help("unsized fn params are gated as an unstable feature");
30823082
}
30833083
}
3084-
ObligationCauseCode::SizedReturnType | ObligationCauseCode::SizedCallReturnType => {
3084+
ObligationCauseCode::SizedReturnType | ObligationCauseCode::SizedCallReturnType(_) => {
30853085
err.note("the return type of a function must have a statically known size");
30863086
}
30873087
ObligationCauseCode::SizedYieldType => {

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ use rustc_middle::ty::{
4848
use rustc_middle::{bug, span_bug};
4949
use rustc_session::Limit;
5050
use rustc_span::def_id::LOCAL_CRATE;
51-
use rustc_span::symbol::sym;
51+
use rustc_span::symbol::{kw, sym};
5252
use rustc_span::{BytePos, ExpnKind, Span, Symbol, DUMMY_SP};
5353
use std::borrow::Cow;
5454
use std::fmt;
@@ -638,6 +638,32 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
638638
err.span_label(span, explanation);
639639
}
640640

641+
if Some(trait_ref.def_id()) == self.tcx.lang_items().sized_trait() {
642+
if !self.tcx.object_safety_violations(trait_ref.def_id()).is_empty() {
643+
match (
644+
obligation.cause.code(),
645+
trait_ref.skip_binder().self_ty().kind(),
646+
) {
647+
(
648+
ObligationCauseCode::WhereClauseInExpr(..),
649+
ty::Dynamic(..),
650+
) => {
651+
return err.delay_as_bug();
652+
}
653+
(ObligationCauseCode::SizedCallReturnType(did), _) => {
654+
let fn_sig = self.tcx.fn_sig(did);
655+
let ret_kind =
656+
fn_sig.skip_binder().output().skip_binder().kind();
657+
if let ty::Param(param_ty) = ret_kind
658+
&& param_ty.name == kw::SelfUpper
659+
{
660+
return err.delay_as_bug();
661+
}
662+
}
663+
_ => {}
664+
}
665+
}
666+
}
641667
if let ObligationCauseCode::Coercion { source, target } =
642668
*obligation.cause.code().peel_derives()
643669
{

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

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,6 @@ 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-
3424
error[E0308]: mismatched types
3525
--> $DIR/trait-missing-dyn-in-qualified-path.rs:5:18
3626
|
@@ -52,16 +42,7 @@ LL | let x: u32 = <Default>::default();
5242
= note: the trait cannot be made into an object because it requires `Self: Sized`
5343
= 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>
5444

55-
error[E0277]: the size for values of type `dyn Default` cannot be known at compilation time
56-
--> $DIR/trait-missing-dyn-in-qualified-path.rs:5:18
57-
|
58-
LL | let x: u32 = <Default>::default();
59-
| ^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
60-
|
61-
= help: the trait `Sized` is not implemented for `dyn Default`
62-
= note: the return type of a function must have a statically known size
63-
64-
error: aborting due to 5 previous errors; 1 warning emitted
45+
error: aborting due to 3 previous errors; 1 warning emitted
6546

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

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,5 @@ fn main() {
88
//[edition2018]~| WARNING this is accepted in the current edition (Rust 2018) but is a hard error in Rust 2021!
99
//[edition2018]~| ERROR trait `Default` cannot be made into an object
1010
//[edition2018]~| ERROR trait `Default` cannot be made into an object
11-
//[edition2018]~| ERROR the size for values of type `dyn Default` cannot be known at compilation time
12-
//[edition2018]~| ERROR the size for values of type `dyn Default` cannot be known at compilation time
1311
//[edition2018]~| ERROR mismatched types
1412
}

tests/ui/traits/bad-sized.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@ 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
98
}

tests/ui/traits/bad-sized.stderr

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,6 @@ 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-
3222
error[E0277]: the size for values of type `dyn Trait` cannot be known at compilation time
3323
--> $DIR/bad-sized.rs:4:37
3424
|
@@ -39,7 +29,7 @@ LL | let x: Vec<dyn Trait + Sized> = Vec::new();
3929
note: required by an implicit `Sized` bound in `Vec`
4030
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
4131

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

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

0 commit comments

Comments
 (0)