Skip to content

Commit 8eb24d0

Browse files
committed
Recognize type Alias = dyn Trait in fn return types
``` error[E0277]: the size for values of type `(dyn Debug + 'static)` cannot be known at compilation time --> $DIR/dyn-trait-type-alias-return-type.rs:4:11 | LL | fn f() -> T { loop {} } | ^ doesn't have a size known at compile-time | = help: the trait `Sized` is not implemented for `(dyn Debug + 'static)` note: this type alias is unsized --> $DIR/dyn-trait-type-alias-return-type.rs:1:1 | LL | type T = dyn core::fmt::Debug; | ^^^^^^ = note: the return type of a function must have a statically known size ```
1 parent 349f572 commit 8eb24d0

File tree

5 files changed

+36
-23
lines changed

5 files changed

+36
-23
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1881,6 +1881,19 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
18811881
let ty::Dynamic(_, _) = trait_pred.self_ty().skip_binder().kind() else {
18821882
return false;
18831883
};
1884+
if let Node::Item(hir::Item { kind: hir::ItemKind::Fn { sig: fn_sig, .. }, .. }) =
1885+
self.tcx.hir_node_by_def_id(obligation.cause.body_id)
1886+
&& let hir::FnRetTy::Return(ty) = fn_sig.decl.output
1887+
&& let hir::TyKind::Path(qpath) = ty.kind
1888+
&& let hir::QPath::Resolved(None, path) = qpath
1889+
&& let Res::Def(DefKind::TyAlias, def_id) = path.res
1890+
{
1891+
// Do not suggest
1892+
// type T = dyn Trait;
1893+
// fn foo() -> impl T { .. }
1894+
err.span_note(self.tcx.def_span(def_id), "this type alias is unsized");
1895+
return false;
1896+
}
18841897

18851898
err.code(E0746);
18861899
err.primary_message("return type cannot be a trait object without pointer indirection");
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
type T = dyn core::fmt::Debug;
2+
//~^ NOTE this type alias is unsized
23

34
fn f() -> T { loop {} }
4-
//~^ ERROR return type cannot be a trait object without pointer indirection
5-
//~| HELP
6-
//~| HELP
5+
//~^ ERROR the size for values of type `(dyn Debug + 'static)` cannot be known at compilation time
6+
//~| HELP the trait `Sized` is not implemented for `(dyn Debug + 'static)`
7+
//~| NOTE doesn't have a size known at compile-time
8+
//~| NOTE the return type of a function must have a statically known size
79

810
fn main() {}
Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
error[E0746]: return type cannot be a trait object without pointer indirection
2-
--> $DIR/dyn-trait-type-alias-return-type.rs:3:11
1+
error[E0277]: the size for values of type `(dyn Debug + 'static)` cannot be known at compilation time
2+
--> $DIR/dyn-trait-type-alias-return-type.rs:4:11
33
|
44
LL | fn f() -> T { loop {} }
55
| ^ doesn't have a size known at compile-time
66
|
7-
help: consider returning an `impl Trait` instead of a `dyn Trait`
7+
= help: the trait `Sized` is not implemented for `(dyn Debug + 'static)`
8+
note: this type alias is unsized
9+
--> $DIR/dyn-trait-type-alias-return-type.rs:1:1
810
|
9-
LL | fn f() -> impl T { loop {} }
10-
| ++++
11-
help: alternatively, box the return type, and wrap all of the returned values in `Box::new`
12-
|
13-
LL | fn f() -> Box<dyn T> { Box::new(loop {}) }
14-
| +++++++ + +++++++++ +
11+
LL | type T = dyn core::fmt::Debug;
12+
| ^^^^^^
13+
= note: the return type of a function must have a statically known size
1514

1615
error: aborting due to 1 previous error
1716

18-
For more information about this error, try `rustc --explain E0746`.
17+
For more information about this error, try `rustc --explain E0277`.

tests/ui/unsized/issue-91801.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub static ALL_VALIDATORS: &[(&'static str, &'static Validator)] =
66
&[("validate that credits and debits balance", &validate_something)];
77

88
fn or<'a>(first: &'static Validator<'a>, second: &'static Validator<'a>) -> Validator<'a> {
9-
//~^ ERROR return type cannot be a trait object without pointer indirection
9+
//~^ ERROR E0277
1010
return Box::new(move |something: &'_ Something| -> Result<(), ()> {
1111
first(something).or_else(|_| second(something))
1212
});
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
error[E0746]: return type cannot be a trait object without pointer indirection
1+
error[E0277]: the size for values of type `(dyn Fn(&'a Something) -> Result<(), ()> + Send + Sync + 'a)` cannot be known at compilation time
22
--> $DIR/issue-91801.rs:8:77
33
|
44
LL | fn or<'a>(first: &'static Validator<'a>, second: &'static Validator<'a>) -> Validator<'a> {
55
| ^^^^^^^^^^^^^ doesn't have a size known at compile-time
66
|
7-
help: consider returning an `impl Trait` instead of a `dyn Trait`
7+
= help: the trait `Sized` is not implemented for `(dyn Fn(&'a Something) -> Result<(), ()> + Send + Sync + 'a)`
8+
note: this type alias is unsized
9+
--> $DIR/issue-91801.rs:3:1
810
|
9-
LL | fn or<'a>(first: &'static Validator<'a>, second: &'static Validator<'a>) -> impl Validator<'a> {
10-
| ++++
11-
help: alternatively, box the return type, and wrap all of the returned values in `Box::new`
12-
|
13-
LL | fn or<'a>(first: &'static Validator<'a>, second: &'static Validator<'a>) -> Box<dyn Validator<'a>> {
14-
| +++++++ +
11+
LL | type Validator<'a> = dyn 'a + Send + Sync + Fn(&'a Something) -> Result<(), ()>;
12+
| ^^^^^^^^^^^^^^^^^^
13+
= note: the return type of a function must have a statically known size
1514

1615
error: aborting due to 1 previous error
1716

18-
For more information about this error, try `rustc --explain E0746`.
17+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)