Skip to content

Commit 2fbde2b

Browse files
authored
Rollup merge of rust-lang#105408 - cuviper:help-rpitirpit, r=compiler-errors
Add help for `#![feature(impl_trait_in_fn_trait_return)]` This adds a new variant `ImplTraitContext::FeatureGated`, so we can generalize the help for `return_position_impl_trait_in_trait` to also work for `impl_trait_in_fn_trait_return`. cc rust-lang#99697
2 parents 4d5a2f3 + e9dd591 commit 2fbde2b

File tree

3 files changed

+40
-17
lines changed

3 files changed

+40
-17
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+21-11
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ enum ImplTraitContext {
259259
},
260260
/// Impl trait in type aliases.
261261
TypeAliasesOpaqueTy,
262+
/// `impl Trait` is unstably accepted in this position.
263+
FeatureGated(ImplTraitPosition, Symbol),
262264
/// `impl Trait` is not accepted in this position.
263265
Disallowed(ImplTraitPosition),
264266
}
@@ -1372,25 +1374,23 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13721374
}
13731375
path
13741376
}
1375-
ImplTraitContext::Disallowed(
1376-
position @ (ImplTraitPosition::TraitReturn | ImplTraitPosition::ImplReturn),
1377-
) => {
1377+
ImplTraitContext::FeatureGated(position, feature) => {
13781378
self.tcx
13791379
.sess
13801380
.create_feature_err(
13811381
MisplacedImplTrait {
13821382
span: t.span,
1383-
position: DiagnosticArgFromDisplay(&position),
1383+
position: DiagnosticArgFromDisplay(position),
13841384
},
1385-
sym::return_position_impl_trait_in_trait,
1385+
*feature,
13861386
)
13871387
.emit();
13881388
hir::TyKind::Err
13891389
}
13901390
ImplTraitContext::Disallowed(position) => {
13911391
self.tcx.sess.emit_err(MisplacedImplTrait {
13921392
span: t.span,
1393-
position: DiagnosticArgFromDisplay(&position),
1393+
position: DiagnosticArgFromDisplay(position),
13941394
});
13951395
hir::TyKind::Err
13961396
}
@@ -1739,14 +1739,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17391739
} else {
17401740
match &decl.output {
17411741
FnRetTy::Ty(ty) => {
1742-
let mut context = if kind.return_impl_trait_allowed(self.tcx) {
1742+
let context = if kind.return_impl_trait_allowed(self.tcx) {
17431743
let fn_def_id = self.local_def_id(fn_node_id);
17441744
ImplTraitContext::ReturnPositionOpaqueTy {
17451745
origin: hir::OpaqueTyOrigin::FnReturn(fn_def_id),
17461746
in_trait: matches!(kind, FnDeclKind::Trait),
17471747
}
17481748
} else {
1749-
ImplTraitContext::Disallowed(match kind {
1749+
let position = match kind {
17501750
FnDeclKind::Fn | FnDeclKind::Inherent => {
17511751
unreachable!("fn should allow in-band lifetimes")
17521752
}
@@ -1755,9 +1755,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17551755
FnDeclKind::Pointer => ImplTraitPosition::PointerReturn,
17561756
FnDeclKind::Trait => ImplTraitPosition::TraitReturn,
17571757
FnDeclKind::Impl => ImplTraitPosition::ImplReturn,
1758-
})
1758+
};
1759+
match kind {
1760+
FnDeclKind::Trait | FnDeclKind::Impl => ImplTraitContext::FeatureGated(
1761+
position,
1762+
sym::return_position_impl_trait_in_trait,
1763+
),
1764+
_ => ImplTraitContext::Disallowed(position),
1765+
}
17591766
};
1760-
hir::FnRetTy::Return(self.lower_ty(ty, &mut context))
1767+
hir::FnRetTy::Return(self.lower_ty(ty, &context))
17611768
}
17621769
FnRetTy::Default(span) => hir::FnRetTy::DefaultReturn(self.lower_span(*span)),
17631770
}
@@ -1938,7 +1945,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19381945
output,
19391946
span,
19401947
if in_trait && !this.tcx.features().return_position_impl_trait_in_trait {
1941-
ImplTraitContext::Disallowed(ImplTraitPosition::TraitReturn)
1948+
ImplTraitContext::FeatureGated(
1949+
ImplTraitPosition::TraitReturn,
1950+
sym::return_position_impl_trait_in_trait,
1951+
)
19421952
} else {
19431953
ImplTraitContext::ReturnPositionOpaqueTy {
19441954
origin: hir::OpaqueTyOrigin::FnReturn(fn_def_id),

compiler/rustc_ast_lowering/src/path.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_ast::{self as ast, *};
99
use rustc_hir as hir;
1010
use rustc_hir::def::{DefKind, PartialRes, Res};
1111
use rustc_hir::GenericArg;
12-
use rustc_span::symbol::{kw, Ident};
12+
use rustc_span::symbol::{kw, sym, Ident};
1313
use rustc_span::{BytePos, Span, DUMMY_SP};
1414

1515
use smallvec::{smallvec, SmallVec};
@@ -352,11 +352,18 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
352352
// fn f(_: impl Fn() -> impl Debug) -> impl Fn() -> impl Debug
353353
// // disallowed --^^^^^^^^^^ allowed --^^^^^^^^^^
354354
// ```
355-
FnRetTy::Ty(ty)
356-
if matches!(itctx, ImplTraitContext::ReturnPositionOpaqueTy { .. })
357-
&& self.tcx.features().impl_trait_in_fn_trait_return =>
358-
{
359-
self.lower_ty(&ty, itctx)
355+
FnRetTy::Ty(ty) if matches!(itctx, ImplTraitContext::ReturnPositionOpaqueTy { .. }) => {
356+
if self.tcx.features().impl_trait_in_fn_trait_return {
357+
self.lower_ty(&ty, itctx)
358+
} else {
359+
self.lower_ty(
360+
&ty,
361+
&ImplTraitContext::FeatureGated(
362+
ImplTraitPosition::FnTraitReturn,
363+
sym::impl_trait_in_fn_trait_return,
364+
),
365+
)
366+
}
360367
}
361368
FnRetTy::Ty(ty) => {
362369
self.lower_ty(&ty, &ImplTraitContext::Disallowed(ImplTraitPosition::FnTraitReturn))

src/test/ui/feature-gates/feature-gate-impl_trait_in_fn_trait_return.stderr

+6
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@ error[E0562]: `impl Trait` only allowed in function and inherent method return t
33
|
44
LL | fn f() -> impl Fn() -> impl Sized { || () }
55
| ^^^^^^^^^^
6+
|
7+
= note: see issue #99697 <https://github.com/rust-lang/rust/issues/99697> for more information
8+
= help: add `#![feature(impl_trait_in_fn_trait_return)]` to the crate attributes to enable
69

710
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in `Fn` trait return
811
--> $DIR/feature-gate-impl_trait_in_fn_trait_return.rs:3:32
912
|
1013
LL | fn g() -> &'static dyn Fn() -> impl Sized { &|| () }
1114
| ^^^^^^^^^^
15+
|
16+
= note: see issue #99697 <https://github.com/rust-lang/rust/issues/99697> for more information
17+
= help: add `#![feature(impl_trait_in_fn_trait_return)]` to the crate attributes to enable
1218

1319
error: aborting due to 2 previous errors
1420

0 commit comments

Comments
 (0)