From a8ad5138619cb2cc963a90c9cd280f892c272af8 Mon Sep 17 00:00:00 2001 From: aerooneqq Date: Tue, 27 Oct 2026 10:49:23 +0000 Subject: [PATCH] delegation: emit error when there is no field for Self mapping --- compiler/rustc_hir_typeck/src/diagnostics.rs | 8 ++ compiler/rustc_hir_typeck/src/expr.rs | 20 ++-- tests/ui/delegation/self-mapping-output.rs | 4 + .../ui/delegation/self-mapping-output.stderr | 110 +++++++++++------- .../self-mapping-zero-fields-infer-ice.rs | 13 +++ .../self-mapping-zero-fields-infer-ice.stderr | 8 ++ 6 files changed, 109 insertions(+), 54 deletions(-) create mode 100644 tests/ui/delegation/self-mapping-zero-fields-infer-ice.rs create mode 100644 tests/ui/delegation/self-mapping-zero-fields-infer-ice.stderr diff --git a/compiler/rustc_hir_typeck/src/diagnostics.rs b/compiler/rustc_hir_typeck/src/diagnostics.rs index 3f06ce69b7c87..3a04a4d28caa8 100644 --- a/compiler/rustc_hir_typeck/src/diagnostics.rs +++ b/compiler/rustc_hir_typeck/src/diagnostics.rs @@ -1394,3 +1394,11 @@ pub(crate) struct WrapConstBlock { #[suggestion_part(code = " }}")] pub right: Span, } + +#[derive(Diagnostic)] +#[diag("the `{$adt_name}` must contain one field for wrapping the return value in delegation")] +pub(crate) struct DelegationInvalidAdtForReturnWrapping { + #[primary_span] + pub span: Span, + pub adt_name: String, +} diff --git a/compiler/rustc_hir_typeck/src/expr.rs b/compiler/rustc_hir_typeck/src/expr.rs index 6059ede2ecdc6..30b42720ed748 100644 --- a/compiler/rustc_hir_typeck/src/expr.rs +++ b/compiler/rustc_hir_typeck/src/expr.rs @@ -45,10 +45,11 @@ use crate::callee::SplatLoweringInfo; use crate::coercion::CoerceMany; use crate::diagnostics::{ AddressOfTemporaryTaken, BaseExpressionDoubleDot, BaseExpressionDoubleDotAddExpr, - BaseExpressionDoubleDotRemove, CantDereference, ExprParenthesesNeeded, - FieldMultiplySpecifiedInInitializer, FunctionalRecordUpdateOnNonStruct, HelpUseLatestEdition, - NakedAsmOutsideNakedFn, NoFieldOnVariant, ReturnLikeStatementKind, ReturnStmtOutsideOfFnBody, - StructExprNonExhaustive, TypeMismatchFruTypo, YieldExprOutsideOfCoroutine, + BaseExpressionDoubleDotRemove, CantDereference, DelegationInvalidAdtForReturnWrapping, + ExprParenthesesNeeded, FieldMultiplySpecifiedInInitializer, FunctionalRecordUpdateOnNonStruct, + HelpUseLatestEdition, NakedAsmOutsideNakedFn, NoFieldOnVariant, ReturnLikeStatementKind, + ReturnStmtOutsideOfFnBody, StructExprNonExhaustive, TypeMismatchFruTypo, + YieldExprOutsideOfCoroutine, }; use crate::op::contains_let_in_chain; use crate::{ @@ -1945,13 +1946,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { if let [v_field] = &variant.fields[..] { Ok(get_field_ident(v_field)) } else { - Err(tcx.dcx().span_delayed_bug( - field.ident.span, - format!( - "can not infer the delegation `Self` mapping field for {:?}", - self.body_def_id - ), - )) + Err(tcx.dcx().emit_err(DelegationInvalidAdtForReturnWrapping { + span: tcx.def_span(self.body_def_id), + adt_name: tcx.def_path_str(adt.did()), + })) } } else { Ok(tcx.adjust_ident(field.ident, variant.def_id)) diff --git a/tests/ui/delegation/self-mapping-output.rs b/tests/ui/delegation/self-mapping-output.rs index 57338a7bd3aa3..925c6a670ab8d 100644 --- a/tests/ui/delegation/self-mapping-output.rs +++ b/tests/ui/delegation/self-mapping-output.rs @@ -137,16 +137,20 @@ mod more_than_one_field { impl Trait for W { reuse Trait::method { self.0 } //~^ ERROR: missing fields `0`, `1` and `2` in initializer of `more_than_one_field::W` + //~| ERROR: the `more_than_one_field::W` must contain one field for wrapping the return value in delegation reuse Trait::r#static; //~^ ERROR: missing fields `0`, `1` and `2` in initializer of `more_than_one_field::W` + //~| ERROR: the `more_than_one_field::W` must contain one field for wrapping the return value in delegation reuse Trait::raw_S { self.0 } } impl W { reuse Trait::method { self.0 } //~^ ERROR: missing fields `0`, `1` and `2` in initializer of `more_than_one_field::W` + //~| ERROR: the `more_than_one_field::W` must contain one field for wrapping the return value in delegation reuse Trait::r#static; //~^ ERROR: missing fields `0`, `1` and `2` in initializer of `more_than_one_field::W` + //~| ERROR: the `more_than_one_field::W` must contain one field for wrapping the return value in delegation reuse Trait::raw_S { self.0 } } } diff --git a/tests/ui/delegation/self-mapping-output.stderr b/tests/ui/delegation/self-mapping-output.stderr index 16d72442dc4af..90155fa62e1aa 100644 --- a/tests/ui/delegation/self-mapping-output.stderr +++ b/tests/ui/delegation/self-mapping-output.stderr @@ -46,32 +46,56 @@ LL - reuse Trait::raw_S { self.0 } LL + reuse Trait::raw_S { self.s } | +error: the `more_than_one_field::W` must contain one field for wrapping the return value in delegation + --> $DIR/self-mapping-output.rs:138:22 + | +LL | reuse Trait::method { self.0 } + | ^^^^^^ + error[E0063]: missing fields `0`, `1` and `2` in initializer of `more_than_one_field::W` --> $DIR/self-mapping-output.rs:138:22 | LL | reuse Trait::method { self.0 } | ^^^^^^ missing `0`, `1` and `2` +error: the `more_than_one_field::W` must contain one field for wrapping the return value in delegation + --> $DIR/self-mapping-output.rs:141:22 + | +LL | reuse Trait::r#static; + | ^^^^^^^^ + error[E0063]: missing fields `0`, `1` and `2` in initializer of `more_than_one_field::W` - --> $DIR/self-mapping-output.rs:140:22 + --> $DIR/self-mapping-output.rs:141:22 | LL | reuse Trait::r#static; | ^^^^^^^^ missing `0`, `1` and `2` +error: the `more_than_one_field::W` must contain one field for wrapping the return value in delegation + --> $DIR/self-mapping-output.rs:148:22 + | +LL | reuse Trait::method { self.0 } + | ^^^^^^ + error[E0063]: missing fields `0`, `1` and `2` in initializer of `more_than_one_field::W` - --> $DIR/self-mapping-output.rs:146:22 + --> $DIR/self-mapping-output.rs:148:22 | LL | reuse Trait::method { self.0 } | ^^^^^^ missing `0`, `1` and `2` +error: the `more_than_one_field::W` must contain one field for wrapping the return value in delegation + --> $DIR/self-mapping-output.rs:151:22 + | +LL | reuse Trait::r#static; + | ^^^^^^^^ + error[E0063]: missing fields `0`, `1` and `2` in initializer of `more_than_one_field::W` - --> $DIR/self-mapping-output.rs:148:22 + --> $DIR/self-mapping-output.rs:151:22 | LL | reuse Trait::r#static; | ^^^^^^^^ missing `0`, `1` and `2` error[E0308]: mismatched types - --> $DIR/self-mapping-output.rs:179:25 + --> $DIR/self-mapping-output.rs:183:25 | LL | pub fn method(_: impl super::Trait) -> impl super::Trait { | ----------------- the found opaque type @@ -86,7 +110,7 @@ LL | reuse to_reuse::method { self.0 } found opaque type `impl non_trait_path_reuse::Trait` error[E0308]: mismatched types - --> $DIR/self-mapping-output.rs:181:25 + --> $DIR/self-mapping-output.rs:185:25 | LL | pub fn r#static() -> impl super::Trait { | ----------------- the found opaque type @@ -101,13 +125,13 @@ LL | reuse to_reuse::r#static; found opaque type `impl non_trait_path_reuse::Trait` error[E0609]: no field `0` on type `impl super::Trait` - --> $DIR/self-mapping-output.rs:186:39 + --> $DIR/self-mapping-output.rs:190:39 | LL | reuse to_reuse::method { self.0 } | ^ unknown field error[E0308]: mismatched types - --> $DIR/self-mapping-output.rs:208:31 + --> $DIR/self-mapping-output.rs:212:31 | LL | reuse Trait::method { self.0 } | ------ ^^^^^^ expected `&_`, found `()` @@ -117,7 +141,7 @@ LL | reuse Trait::method { self.0 } = note: expected reference `&_` found unit type `()` note: method defined here - --> $DIR/self-mapping-output.rs:194:12 + --> $DIR/self-mapping-output.rs:198:12 | LL | trait Trait { LL | fn method(&self) -> (); @@ -128,14 +152,14 @@ LL | reuse Trait::method { &self.0 } | + error[E0283]: type annotations needed - --> $DIR/self-mapping-output.rs:211:22 + --> $DIR/self-mapping-output.rs:215:22 | LL | reuse Trait::r#static; | ^^^^^^^^ cannot infer type | = note: the type must implement `non_Self_return_type::Trait` help: the following types implement trait `non_Self_return_type::Trait` - --> $DIR/self-mapping-output.rs:200:5 + --> $DIR/self-mapping-output.rs:204:5 | LL | impl Trait for S { | ^^^^^^^^^^^^^^^^ `non_Self_return_type::S` @@ -144,7 +168,7 @@ LL | impl Trait for W { | ^^^^^^^^^^^^^^^^ `non_Self_return_type::W` error[E0308]: mismatched types - --> $DIR/self-mapping-output.rs:214:30 + --> $DIR/self-mapping-output.rs:218:30 | LL | reuse Trait::raw_S { self.0 } | ----- ^^^^^^ expected `&_`, found `()` @@ -154,7 +178,7 @@ LL | reuse Trait::raw_S { self.0 } = note: expected reference `&_` found unit type `()` note: method defined here - --> $DIR/self-mapping-output.rs:196:12 + --> $DIR/self-mapping-output.rs:200:12 | LL | trait Trait { ... @@ -166,7 +190,7 @@ LL | reuse Trait::raw_S { &self.0 } | + error[E0308]: mismatched types - --> $DIR/self-mapping-output.rs:219:31 + --> $DIR/self-mapping-output.rs:223:31 | LL | reuse Trait::method { self.0 } | ------ ^^^^^^ expected `&_`, found `()` @@ -176,7 +200,7 @@ LL | reuse Trait::method { self.0 } = note: expected reference `&_` found unit type `()` note: method defined here - --> $DIR/self-mapping-output.rs:194:12 + --> $DIR/self-mapping-output.rs:198:12 | LL | trait Trait { LL | fn method(&self) -> (); @@ -187,14 +211,14 @@ LL | reuse Trait::method { &self.0 } | + error[E0283]: type annotations needed - --> $DIR/self-mapping-output.rs:222:22 + --> $DIR/self-mapping-output.rs:226:22 | LL | reuse Trait::r#static; | ^^^^^^^^ cannot infer type | = note: the type must implement `non_Self_return_type::Trait` help: the following types implement trait `non_Self_return_type::Trait` - --> $DIR/self-mapping-output.rs:200:5 + --> $DIR/self-mapping-output.rs:204:5 | LL | impl Trait for S { | ^^^^^^^^^^^^^^^^ `non_Self_return_type::S` @@ -203,7 +227,7 @@ LL | impl Trait for W { | ^^^^^^^^^^^^^^^^ `non_Self_return_type::W` error[E0308]: mismatched types - --> $DIR/self-mapping-output.rs:225:30 + --> $DIR/self-mapping-output.rs:229:30 | LL | reuse Trait::raw_S { self.0 } | ----- ^^^^^^ expected `&_`, found `()` @@ -213,7 +237,7 @@ LL | reuse Trait::raw_S { self.0 } = note: expected reference `&_` found unit type `()` note: method defined here - --> $DIR/self-mapping-output.rs:196:12 + --> $DIR/self-mapping-output.rs:200:12 | LL | trait Trait { ... @@ -225,7 +249,7 @@ LL | reuse Trait::raw_S { &self.0 } | + error[E0308]: mismatched types - --> $DIR/self-mapping-output.rs:251:38 + --> $DIR/self-mapping-output.rs:255:38 | LL | reuse ::method { self.0 } | ------ ^^^^^^ expected `&F`, found `&S` @@ -236,31 +260,31 @@ LL | reuse ::method { self.0 } = note: expected reference `&wrong_return_type::F` found reference `&wrong_return_type::S` note: method defined here - --> $DIR/self-mapping-output.rs:232:12 + --> $DIR/self-mapping-output.rs:236:12 | LL | trait Trait { LL | fn method(&self) -> Self; | ^^^^^^ ---- note: method provided through this `impl` - --> $DIR/self-mapping-output.rs:238:5 + --> $DIR/self-mapping-output.rs:242:5 | LL | impl Trait for F { | ^^^^^^^^^^^^^^^^ error[E0308]: mismatched types - --> $DIR/self-mapping-output.rs:251:29 + --> $DIR/self-mapping-output.rs:255:29 | LL | reuse ::method { self.0 } | ^^^^^^ expected `S`, found `F` error[E0308]: mismatched types - --> $DIR/self-mapping-output.rs:255:29 + --> $DIR/self-mapping-output.rs:259:29 | LL | reuse ::r#static; | ^^^^^^^^ expected `S`, found `F` error[E0308]: mismatched types - --> $DIR/self-mapping-output.rs:258:37 + --> $DIR/self-mapping-output.rs:262:37 | LL | reuse ::raw_S { self.0 } | ----- ^^^^^^ expected `&F`, found `&S` @@ -270,20 +294,20 @@ LL | reuse ::raw_S { self.0 } = note: expected reference `&wrong_return_type::F` found reference `&wrong_return_type::S` note: method defined here - --> $DIR/self-mapping-output.rs:234:12 + --> $DIR/self-mapping-output.rs:238:12 | LL | trait Trait { ... LL | fn raw_S(&self) -> S { S } | ^^^^^ ----- note: method provided through this `impl` - --> $DIR/self-mapping-output.rs:238:5 + --> $DIR/self-mapping-output.rs:242:5 | LL | impl Trait for F { | ^^^^^^^^^^^^^^^^ error[E0308]: mismatched types - --> $DIR/self-mapping-output.rs:263:38 + --> $DIR/self-mapping-output.rs:267:38 | LL | reuse ::method { self.0 } | ------ ^^^^^^ expected `&F`, found `&S` @@ -294,31 +318,31 @@ LL | reuse ::method { self.0 } = note: expected reference `&wrong_return_type::F` found reference `&wrong_return_type::S` note: method defined here - --> $DIR/self-mapping-output.rs:232:12 + --> $DIR/self-mapping-output.rs:236:12 | LL | trait Trait { LL | fn method(&self) -> Self; | ^^^^^^ ---- note: method provided through this `impl` - --> $DIR/self-mapping-output.rs:238:5 + --> $DIR/self-mapping-output.rs:242:5 | LL | impl Trait for F { | ^^^^^^^^^^^^^^^^ error[E0308]: mismatched types - --> $DIR/self-mapping-output.rs:263:29 + --> $DIR/self-mapping-output.rs:267:29 | LL | reuse ::method { self.0 } | ^^^^^^ expected `S`, found `F` error[E0308]: mismatched types - --> $DIR/self-mapping-output.rs:267:29 + --> $DIR/self-mapping-output.rs:271:29 | LL | reuse ::r#static; | ^^^^^^^^ expected `S`, found `F` error[E0308]: mismatched types - --> $DIR/self-mapping-output.rs:270:37 + --> $DIR/self-mapping-output.rs:274:37 | LL | reuse ::raw_S { self.0 } | ----- ^^^^^^ expected `&F`, found `&S` @@ -328,20 +352,20 @@ LL | reuse ::raw_S { self.0 } = note: expected reference `&wrong_return_type::F` found reference `&wrong_return_type::S` note: method defined here - --> $DIR/self-mapping-output.rs:234:12 + --> $DIR/self-mapping-output.rs:238:12 | LL | trait Trait { ... LL | fn raw_S(&self) -> S { S } | ^^^^^ ----- note: method provided through this `impl` - --> $DIR/self-mapping-output.rs:238:5 + --> $DIR/self-mapping-output.rs:242:5 | LL | impl Trait for F { | ^^^^^^^^^^^^^^^^ error[E0308]: mismatched types - --> $DIR/self-mapping-output.rs:296:31 + --> $DIR/self-mapping-output.rs:300:31 | LL | reuse Trait::method { F } | ------ ^ expected `&S`, found `&F` @@ -352,14 +376,14 @@ LL | reuse Trait::method { F } = note: expected reference `&wrong_target_expression::S` found reference `&wrong_target_expression::F` note: method defined here - --> $DIR/self-mapping-output.rs:277:12 + --> $DIR/self-mapping-output.rs:281:12 | LL | trait Trait { LL | fn method(&self) -> Self; | ^^^^^^ ---- error[E0308]: mismatched types - --> $DIR/self-mapping-output.rs:304:31 + --> $DIR/self-mapping-output.rs:308:31 | LL | reuse Trait::method { F } | ------ ^ expected `&S`, found `&F` @@ -370,37 +394,37 @@ LL | reuse Trait::method { F } = note: expected reference `&wrong_target_expression::S` found reference `&wrong_target_expression::F` note: method defined here - --> $DIR/self-mapping-output.rs:277:12 + --> $DIR/self-mapping-output.rs:281:12 | LL | trait Trait { LL | fn method(&self) -> Self; | ^^^^^^ ---- error[E0616]: field `0` of struct `private::W` is private - --> $DIR/self-mapping-output.rs:330:36 + --> $DIR/self-mapping-output.rs:334:36 | LL | reuse Trait::method { self.0 } | ^ private field error[E0616]: field `0` of struct `private::W` is private - --> $DIR/self-mapping-output.rs:335:35 + --> $DIR/self-mapping-output.rs:339:35 | LL | reuse Trait::raw_S { self.0 } | ^ private field error[E0616]: field `0` of struct `private::W` is private - --> $DIR/self-mapping-output.rs:340:36 + --> $DIR/self-mapping-output.rs:344:36 | LL | reuse Trait::method { self.0 } | ^ private field error[E0616]: field `0` of struct `private::W` is private - --> $DIR/self-mapping-output.rs:345:35 + --> $DIR/self-mapping-output.rs:349:35 | LL | reuse Trait::raw_S { self.0 } | ^ private field -error: aborting due to 31 previous errors +error: aborting due to 35 previous errors Some errors have detailed explanations: E0063, E0283, E0308, E0609, E0616. For more information about an error, try `rustc --explain E0063`. diff --git a/tests/ui/delegation/self-mapping-zero-fields-infer-ice.rs b/tests/ui/delegation/self-mapping-zero-fields-infer-ice.rs new file mode 100644 index 0000000000000..764bb53a9b666 --- /dev/null +++ b/tests/ui/delegation/self-mapping-zero-fields-infer-ice.rs @@ -0,0 +1,13 @@ +#![feature(fn_delegation)] + +trait Trait { + fn add(self) -> Self; +} + +struct S; +impl S { + reuse Trait::*; + //~^ ERROR: the `S` must contain one field for wrapping the return value in delegation +} + +fn main() {} diff --git a/tests/ui/delegation/self-mapping-zero-fields-infer-ice.stderr b/tests/ui/delegation/self-mapping-zero-fields-infer-ice.stderr new file mode 100644 index 0000000000000..90f60886a1246 --- /dev/null +++ b/tests/ui/delegation/self-mapping-zero-fields-infer-ice.stderr @@ -0,0 +1,8 @@ +error: the `S` must contain one field for wrapping the return value in delegation + --> $DIR/self-mapping-zero-fields-infer-ice.rs:9:18 + | +LL | reuse Trait::*; + | ^ + +error: aborting due to 1 previous error +