Skip to content

Commit

Permalink
Make error message less awkward
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Feb 3, 2025
1 parent 7bc0b3c commit 052dfa7
Show file tree
Hide file tree
Showing 16 changed files with 32 additions and 32 deletions.
12 changes: 6 additions & 6 deletions compiler/rustc_hir_typeck/src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,17 @@ pub(super) fn check_fn<'a, 'tcx>(

fcx.typeck_results.borrow_mut().liberated_fn_sigs_mut().insert(fn_id, fn_sig);

let return_or_body_span = match decl.output {
hir::FnRetTy::DefaultReturn(_) => body.value.span,
hir::FnRetTy::Return(ty) => ty.span,
};

// We checked the root's ret ty during wfcheck, but not the child.
if fcx.tcx.is_typeck_child(fn_def_id.to_def_id()) {
let return_or_body_span = match decl.output {
hir::FnRetTy::DefaultReturn(_) => body.value.span,
hir::FnRetTy::Return(ty) => ty.span,
};

fcx.require_type_is_sized(
declared_ret_ty,
return_or_body_span,
ObligationCauseCode::WellFormed(None),
ObligationCauseCode::SizedReturnType,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1765,7 +1765,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
};

err.code(E0746);
err.primary_message("return type cannot have an unboxed trait object");
err.primary_message("return type cannot be a trait object without pointer indirection");
err.children.clear();

let span = obligation.cause.span;
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/error-codes/E0746.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0746]: return type cannot have an unboxed trait object
error[E0746]: return type cannot be a trait object without pointer indirection
--> $DIR/E0746.rs:8:13
|
LL | fn foo() -> dyn Trait { Struct }
Expand All @@ -13,7 +13,7 @@ help: alternatively, box the return type, and wrap all of the returned values in
LL | fn foo() -> Box<dyn Trait> { Box::new(Struct) }
| ++++ + +++++++++ +

error[E0746]: return type cannot have an unboxed trait object
error[E0746]: return type cannot be a trait object without pointer indirection
--> $DIR/E0746.rs:11:13
|
LL | fn bar() -> dyn Trait {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl DynIncompatible for B {
}

fn car() -> dyn DynIncompatible { //~ ERROR the trait `DynIncompatible` is not dyn compatible
//~^ ERROR return type cannot have an unboxed trait object
//~^ ERROR return type cannot be a trait object without pointer indirection
if true {
return A;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ help: alternatively, consider constraining `foo` so it does not apply to trait o
LL | fn foo() -> Self where Self: Sized;
| +++++++++++++++++

error[E0746]: return type cannot have an unboxed trait object
error[E0746]: return type cannot be a trait object without pointer indirection
--> $DIR/dyn-incompatible-trait-in-return-position-dyn-trait.rs:22:13
|
LL | fn car() -> dyn DynIncompatible {
Expand Down
14 changes: 7 additions & 7 deletions tests/ui/impl-trait/dyn-trait-return-should-be-impl-trait.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ LL | fn bar() -> (usize, dyn Trait) { (42, Struct) }
= note: required because it appears within the type `(usize, (dyn Trait + 'static))`
= note: the return type of a function must have a statically known size

error[E0746]: return type cannot have an unboxed trait object
error[E0746]: return type cannot be a trait object without pointer indirection
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:15:13
|
LL | fn bap() -> Trait { Struct }
Expand All @@ -33,7 +33,7 @@ help: alternatively, box the return type, and wrap all of the returned values in
LL | fn bap() -> Box<dyn Trait> { Box::new(Struct) }
| +++++++ + +++++++++ +

error[E0746]: return type cannot have an unboxed trait object
error[E0746]: return type cannot be a trait object without pointer indirection
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:17:13
|
LL | fn ban() -> dyn Trait { Struct }
Expand All @@ -48,7 +48,7 @@ help: alternatively, box the return type, and wrap all of the returned values in
LL | fn ban() -> Box<dyn Trait> { Box::new(Struct) }
| ++++ + +++++++++ +

error[E0746]: return type cannot have an unboxed trait object
error[E0746]: return type cannot be a trait object without pointer indirection
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:19:13
|
LL | fn bak() -> dyn Trait { unimplemented!() }
Expand All @@ -63,7 +63,7 @@ help: alternatively, box the return type, and wrap all of the returned values in
LL | fn bak() -> Box<dyn Trait> { Box::new(unimplemented!()) }
| ++++ + +++++++++ +

error[E0746]: return type cannot have an unboxed trait object
error[E0746]: return type cannot be a trait object without pointer indirection
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:21:13
|
LL | fn bal() -> dyn Trait {
Expand All @@ -82,7 +82,7 @@ LL | }
LL ~ Box::new(42)
|

error[E0746]: return type cannot have an unboxed trait object
error[E0746]: return type cannot be a trait object without pointer indirection
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:27:13
|
LL | fn bax() -> dyn Trait {
Expand All @@ -101,7 +101,7 @@ LL | } else {
LL ~ Box::new(42)
|

error[E0746]: return type cannot have an unboxed trait object
error[E0746]: return type cannot be a trait object without pointer indirection
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:62:13
|
LL | fn bat() -> dyn Trait {
Expand All @@ -120,7 +120,7 @@ LL | }
LL ~ Box::new(42)
|

error[E0746]: return type cannot have an unboxed trait object
error[E0746]: return type cannot be a trait object without pointer indirection
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:68:13
|
LL | fn bay() -> dyn Trait {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fn dog() -> impl std::fmt::Display {
}
}

fn hat() -> dyn std::fmt::Display { //~ ERROR return type cannot have an unboxed trait object
fn hat() -> dyn std::fmt::Display { //~ ERROR return type cannot be a trait object without pointer indirection
match 13 {
0 => {
return 0i32;
Expand All @@ -74,15 +74,15 @@ fn hat() -> dyn std::fmt::Display { //~ ERROR return type cannot have an unboxed
}
}

fn pug() -> dyn std::fmt::Display { //~ ERROR return type cannot have an unboxed trait object
fn pug() -> dyn std::fmt::Display { //~ ERROR return type cannot be a trait object without pointer indirection
match 13 {
0 => 0i32,
1 => 1u32,
_ => 2u32,
}
}

fn man() -> dyn std::fmt::Display { //~ ERROR return type cannot have an unboxed trait object
fn man() -> dyn std::fmt::Display { //~ ERROR return type cannot be a trait object without pointer indirection
if false {
0i32
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0746]: return type cannot have an unboxed trait object
error[E0746]: return type cannot be a trait object without pointer indirection
--> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:66:13
|
LL | fn hat() -> dyn std::fmt::Display {
Expand All @@ -19,7 +19,7 @@ LL | _ => {
LL ~ Box::new(1u32)
|

error[E0746]: return type cannot have an unboxed trait object
error[E0746]: return type cannot be a trait object without pointer indirection
--> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:77:13
|
LL | fn pug() -> dyn std::fmt::Display {
Expand All @@ -38,7 +38,7 @@ LL ~ 1 => Box::new(1u32),
LL ~ _ => Box::new(2u32),
|

error[E0746]: return type cannot have an unboxed trait object
error[E0746]: return type cannot be a trait object without pointer indirection
--> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:85:13
|
LL | fn man() -> dyn std::fmt::Display {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/issues/issue-18107.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub trait AbstractRenderer {}

fn _create_render(_: &()) ->
dyn AbstractRenderer
//~^ ERROR return type cannot have an unboxed trait object
//~^ ERROR return type cannot be a trait object without pointer indirection
{
match 0 {
_ => unimplemented!()
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/issues/issue-18107.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0746]: return type cannot have an unboxed trait object
error[E0746]: return type cannot be a trait object without pointer indirection
--> $DIR/issue-18107.rs:4:5
|
LL | dyn AbstractRenderer
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/unsized/box-instead-of-dyn-fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fmt::Debug;
// Test to suggest boxing the return type, and the closure branch of the `if`

fn print_on_or_the_other<'a>(a: i32, b: &'a String) -> dyn Fn() + 'a {
//~^ ERROR return type cannot have an unboxed trait object
//~^ ERROR return type cannot be a trait object without pointer indirection
if a % 2 == 0 {
move || println!("{a}")
} else {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/unsized/box-instead-of-dyn-fn.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0746]: return type cannot have an unboxed trait object
error[E0746]: return type cannot be a trait object without pointer indirection
--> $DIR/box-instead-of-dyn-fn.rs:5:56
|
LL | fn print_on_or_the_other<'a>(a: i32, b: &'a String) -> dyn Fn() + 'a {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/unsized/issue-91801.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub static ALL_VALIDATORS: &[(&'static str, &'static Validator)] =
&[("validate that credits and debits balance", &validate_something)];

fn or<'a>(first: &'static Validator<'a>, second: &'static Validator<'a>) -> Validator<'a> {
//~^ ERROR return type cannot have an unboxed trait object
//~^ ERROR return type cannot be a trait object without pointer indirection
return Box::new(move |something: &'_ Something| -> Result<(), ()> {
first(something).or_else(|_| second(something))
});
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/unsized/issue-91801.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0746]: return type cannot have an unboxed trait object
error[E0746]: return type cannot be a trait object without pointer indirection
--> $DIR/issue-91801.rs:8:77
|
LL | fn or<'a>(first: &'static Validator<'a>, second: &'static Validator<'a>) -> Validator<'a> {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/unsized/issue-91803.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
trait Foo<'a> {}

fn or<'a>(first: &'static dyn Foo<'a>) -> dyn Foo<'a> {
//~^ ERROR return type cannot have an unboxed trait object
//~^ ERROR return type cannot be a trait object without pointer indirection
return Box::new(panic!());
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/unsized/issue-91803.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0746]: return type cannot have an unboxed trait object
error[E0746]: return type cannot be a trait object without pointer indirection
--> $DIR/issue-91803.rs:3:43
|
LL | fn or<'a>(first: &'static dyn Foo<'a>) -> dyn Foo<'a> {
Expand Down

0 comments on commit 052dfa7

Please sign in to comment.