Skip to content

Commit 7cd466a

Browse files
Move in_trait into OpaqueTyOrigin
1 parent cb7e369 commit 7cd466a

File tree

19 files changed

+146
-125
lines changed

19 files changed

+146
-125
lines changed

Diff for: compiler/rustc_ast_lowering/src/item.rs

-2
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
286286
parent: this.local_def_id(id),
287287
in_assoc_ty: false,
288288
},
289-
fn_kind: None,
290289
}),
291290
},
292291
);
@@ -983,7 +982,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
983982
parent: this.local_def_id(i.id),
984983
in_assoc_ty: true,
985984
},
986-
fn_kind: None,
987985
});
988986
hir::ImplItemKind::Type(ty)
989987
}

Diff for: compiler/rustc_ast_lowering/src/lib.rs

+37-43
Original file line numberDiff line numberDiff line change
@@ -288,12 +288,7 @@ enum ImplTraitContext {
288288
/// Example: `fn foo() -> impl Debug`, where `impl Debug` is conceptually
289289
/// equivalent to a new opaque type like `type T = impl Debug; fn foo() -> T`.
290290
///
291-
OpaqueTy {
292-
origin: hir::OpaqueTyOrigin,
293-
/// Only used to change the lifetime capture rules, since
294-
/// RPITIT captures all in scope, RPIT does not.
295-
fn_kind: Option<FnDeclKind>,
296-
},
291+
OpaqueTy { origin: hir::OpaqueTyOrigin },
297292
/// `impl Trait` is unstably accepted in this position.
298293
FeatureGated(ImplTraitPosition, Symbol),
299294
/// `impl Trait` is not accepted in this position.
@@ -1404,14 +1399,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14041399
TyKind::ImplTrait(def_node_id, bounds) => {
14051400
let span = t.span;
14061401
match itctx {
1407-
ImplTraitContext::OpaqueTy { origin, fn_kind } => self.lower_opaque_impl_trait(
1408-
span,
1409-
origin,
1410-
*def_node_id,
1411-
bounds,
1412-
fn_kind,
1413-
itctx,
1414-
),
1402+
ImplTraitContext::OpaqueTy { origin } => {
1403+
self.lower_opaque_impl_trait(span, origin, *def_node_id, bounds, itctx)
1404+
}
14151405
ImplTraitContext::Universal => {
14161406
if let Some(span) = bounds.iter().find_map(|bound| match *bound {
14171407
ast::GenericBound::Use(_, span) => Some(span),
@@ -1513,7 +1503,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15131503
origin: hir::OpaqueTyOrigin,
15141504
opaque_ty_node_id: NodeId,
15151505
bounds: &GenericBounds,
1516-
fn_kind: Option<FnDeclKind>,
15171506
itctx: ImplTraitContext,
15181507
) -> hir::TyKind<'hir> {
15191508
// Make sure we know that some funky desugaring has been going on here.
@@ -1555,11 +1544,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15551544
.map(|(ident, id, _)| Lifetime { id, ident })
15561545
.collect()
15571546
}
1558-
hir::OpaqueTyOrigin::FnReturn { .. } => {
1559-
if matches!(
1560-
fn_kind.expect("expected RPITs to be lowered with a FnKind"),
1561-
FnDeclKind::Impl | FnDeclKind::Trait
1562-
) || self.tcx.features().lifetime_capture_rules_2024
1547+
hir::OpaqueTyOrigin::FnReturn { in_trait_or_impl, .. } => {
1548+
if in_trait_or_impl.is_some()
1549+
|| self.tcx.features().lifetime_capture_rules_2024
15631550
|| span.at_least_rust_2024()
15641551
{
15651552
// return-position impl trait in trait was decided to capture all
@@ -1583,30 +1570,22 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15831570
};
15841571
debug!(?captured_lifetimes_to_duplicate);
15851572

1586-
match fn_kind {
1587-
// Deny `use<>` on RPITIT in trait/trait-impl for now.
1588-
Some(FnDeclKind::Trait | FnDeclKind::Impl) => {
1573+
// Feature gate for RPITIT + use<..>
1574+
match origin {
1575+
rustc_hir::OpaqueTyOrigin::FnReturn { in_trait_or_impl: Some(_), .. } => {
15891576
if let Some(span) = bounds.iter().find_map(|bound| match *bound {
15901577
ast::GenericBound::Use(_, span) => Some(span),
15911578
_ => None,
15921579
}) {
15931580
self.tcx.dcx().emit_err(errors::NoPreciseCapturesOnRpitit { span });
15941581
}
15951582
}
1596-
None
1597-
| Some(
1598-
FnDeclKind::Fn
1599-
| FnDeclKind::Inherent
1600-
| FnDeclKind::ExternFn
1601-
| FnDeclKind::Closure
1602-
| FnDeclKind::Pointer,
1603-
) => {}
1583+
_ => {}
16041584
}
16051585

16061586
self.lower_opaque_inner(
16071587
opaque_ty_node_id,
16081588
origin,
1609-
matches!(fn_kind, Some(FnDeclKind::Trait)),
16101589
captured_lifetimes_to_duplicate,
16111590
span,
16121591
opaque_ty_span,
@@ -1618,7 +1597,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
16181597
&mut self,
16191598
opaque_ty_node_id: NodeId,
16201599
origin: hir::OpaqueTyOrigin,
1621-
in_trait: bool,
16221600
captured_lifetimes_to_duplicate: FxIndexSet<Lifetime>,
16231601
span: Span,
16241602
opaque_ty_span: Span,
@@ -1747,7 +1725,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17471725
bounds,
17481726
origin,
17491727
lifetime_mapping,
1750-
in_trait,
17511728
};
17521729

17531730
// Generate an `type Foo = impl Trait;` declaration.
@@ -1863,14 +1840,23 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
18631840
None => match &decl.output {
18641841
FnRetTy::Ty(ty) => {
18651842
let itctx = match kind {
1866-
FnDeclKind::Fn
1867-
| FnDeclKind::Inherent
1868-
| FnDeclKind::Trait
1869-
| FnDeclKind::Impl => ImplTraitContext::OpaqueTy {
1843+
FnDeclKind::Fn | FnDeclKind::Inherent => ImplTraitContext::OpaqueTy {
1844+
origin: hir::OpaqueTyOrigin::FnReturn {
1845+
parent: self.local_def_id(fn_node_id),
1846+
in_trait_or_impl: None,
1847+
},
1848+
},
1849+
FnDeclKind::Trait => ImplTraitContext::OpaqueTy {
18701850
origin: hir::OpaqueTyOrigin::FnReturn {
18711851
parent: self.local_def_id(fn_node_id),
1852+
in_trait_or_impl: Some(hir::RpitContext::Trait),
1853+
},
1854+
},
1855+
FnDeclKind::Impl => ImplTraitContext::OpaqueTy {
1856+
origin: hir::OpaqueTyOrigin::FnReturn {
1857+
parent: self.local_def_id(fn_node_id),
1858+
in_trait_or_impl: Some(hir::RpitContext::TraitImpl),
18721859
},
1873-
fn_kind: Some(kind),
18741860
},
18751861
FnDeclKind::ExternFn => {
18761862
ImplTraitContext::Disallowed(ImplTraitPosition::ExternFnReturn)
@@ -1952,10 +1938,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19521938
.map(|(ident, id, _)| Lifetime { id, ident })
19531939
.collect();
19541940

1941+
let in_trait_or_impl = match fn_kind {
1942+
FnDeclKind::Trait => Some(hir::RpitContext::Trait),
1943+
FnDeclKind::Impl => Some(hir::RpitContext::TraitImpl),
1944+
FnDeclKind::Fn | FnDeclKind::Inherent => None,
1945+
FnDeclKind::ExternFn | FnDeclKind::Closure | FnDeclKind::Pointer => unreachable!(),
1946+
};
1947+
19551948
let opaque_ty_ref = self.lower_opaque_inner(
19561949
opaque_ty_node_id,
1957-
hir::OpaqueTyOrigin::AsyncFn { parent: fn_def_id },
1958-
matches!(fn_kind, FnDeclKind::Trait),
1950+
hir::OpaqueTyOrigin::AsyncFn { parent: fn_def_id, in_trait_or_impl },
19591951
captured_lifetimes,
19601952
span,
19611953
opaque_ty_span,
@@ -1965,8 +1957,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19651957
coro,
19661958
opaque_ty_span,
19671959
ImplTraitContext::OpaqueTy {
1968-
origin: hir::OpaqueTyOrigin::FnReturn { parent: fn_def_id },
1969-
fn_kind: Some(fn_kind),
1960+
origin: hir::OpaqueTyOrigin::FnReturn {
1961+
parent: fn_def_id,
1962+
in_trait_or_impl,
1963+
},
19701964
},
19711965
);
19721966
arena_vec![this; bound]

Diff for: compiler/rustc_borrowck/src/region_infer/opaque_types.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -503,8 +503,8 @@ impl<'tcx> LazyOpaqueTyEnv<'tcx> {
503503
let &Self { tcx, def_id, .. } = self;
504504
let origin = tcx.opaque_type_origin(def_id);
505505
let parent = match origin {
506-
hir::OpaqueTyOrigin::FnReturn { parent }
507-
| hir::OpaqueTyOrigin::AsyncFn { parent }
506+
hir::OpaqueTyOrigin::FnReturn { parent, .. }
507+
| hir::OpaqueTyOrigin::AsyncFn { parent, .. }
508508
| hir::OpaqueTyOrigin::TyAlias { parent, .. } => parent,
509509
};
510510
let param_env = tcx.param_env(parent);

Diff for: compiler/rustc_hir/src/hir.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -2762,10 +2762,6 @@ pub struct OpaqueTy<'hir> {
27622762
/// This mapping associated a captured lifetime (first parameter) with the new
27632763
/// early-bound lifetime that was generated for the opaque.
27642764
pub lifetime_mapping: &'hir [(&'hir Lifetime, LocalDefId)],
2765-
/// Whether the opaque is a return-position impl trait (or async future)
2766-
/// originating from a trait method. This makes it so that the opaque is
2767-
/// lowered as an associated type.
2768-
pub in_trait: bool,
27692765
}
27702766

27712767
#[derive(Debug, Clone, Copy, HashStable_Generic)]
@@ -2802,18 +2798,28 @@ pub struct PreciseCapturingNonLifetimeArg {
28022798
pub res: Res,
28032799
}
28042800

2801+
#[derive(Copy, Clone, PartialEq, Eq, Debug, HashStable_Generic)]
2802+
pub enum RpitContext {
2803+
Trait,
2804+
TraitImpl,
2805+
}
2806+
28052807
/// From whence the opaque type came.
28062808
#[derive(Copy, Clone, PartialEq, Eq, Debug, HashStable_Generic)]
28072809
pub enum OpaqueTyOrigin {
28082810
/// `-> impl Trait`
28092811
FnReturn {
28102812
/// The defining function.
28112813
parent: LocalDefId,
2814+
// Whether this is an RPITIT (return position impl trait in trait)
2815+
in_trait_or_impl: Option<RpitContext>,
28122816
},
28132817
/// `async fn`
28142818
AsyncFn {
28152819
/// The defining function.
28162820
parent: LocalDefId,
2821+
// Whether this is an AFIT (async fn in trait)
2822+
in_trait_or_impl: Option<RpitContext>,
28172823
},
28182824
/// type aliases: `type Foo = impl Trait;`
28192825
TyAlias {

Diff for: compiler/rustc_hir_analysis/src/check/check.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,8 @@ fn check_opaque_meets_bounds<'tcx>(
336336
origin: &hir::OpaqueTyOrigin,
337337
) -> Result<(), ErrorGuaranteed> {
338338
let defining_use_anchor = match *origin {
339-
hir::OpaqueTyOrigin::FnReturn { parent }
340-
| hir::OpaqueTyOrigin::AsyncFn { parent }
339+
hir::OpaqueTyOrigin::FnReturn { parent, .. }
340+
| hir::OpaqueTyOrigin::AsyncFn { parent, .. }
341341
| hir::OpaqueTyOrigin::TyAlias { parent, .. } => parent,
342342
};
343343
let param_env = tcx.param_env(defining_use_anchor);
@@ -346,8 +346,8 @@ fn check_opaque_meets_bounds<'tcx>(
346346
let ocx = ObligationCtxt::new_with_diagnostics(&infcx);
347347

348348
let args = match *origin {
349-
hir::OpaqueTyOrigin::FnReturn { parent }
350-
| hir::OpaqueTyOrigin::AsyncFn { parent }
349+
hir::OpaqueTyOrigin::FnReturn { parent, .. }
350+
| hir::OpaqueTyOrigin::AsyncFn { parent, .. }
351351
| hir::OpaqueTyOrigin::TyAlias { parent, .. } => GenericArgs::identity_for_item(
352352
tcx, parent,
353353
)
@@ -736,8 +736,8 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
736736
check_opaque_precise_captures(tcx, def_id);
737737

738738
let origin = tcx.opaque_type_origin(def_id);
739-
if let hir::OpaqueTyOrigin::FnReturn { parent: fn_def_id }
740-
| hir::OpaqueTyOrigin::AsyncFn { parent: fn_def_id } = origin
739+
if let hir::OpaqueTyOrigin::FnReturn { parent: fn_def_id, .. }
740+
| hir::OpaqueTyOrigin::AsyncFn { parent: fn_def_id, .. } = origin
741741
&& let hir::Node::TraitItem(trait_item) = tcx.hir_node_by_def_id(fn_def_id)
742742
&& let (_, hir::TraitFn::Required(..)) = trait_item.expect_fn()
743743
{

Diff for: compiler/rustc_hir_analysis/src/check/compare_impl_item/refine.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ pub(super) fn check_refining_return_position_impl_trait_in_trait<'tcx>(
9494
if !tcx.hir().get_if_local(impl_opaque.def_id).is_some_and(|node| {
9595
matches!(
9696
node.expect_item().expect_opaque_ty().origin,
97-
hir::OpaqueTyOrigin::AsyncFn { parent } | hir::OpaqueTyOrigin::FnReturn { parent }
97+
hir::OpaqueTyOrigin::AsyncFn { parent, .. } | hir::OpaqueTyOrigin::FnReturn { parent, .. }
9898
if parent == impl_m.def_id.expect_local()
9999
)
100100
}) {

Diff for: compiler/rustc_hir_analysis/src/collect/generics_of.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -210,12 +210,11 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
210210
Node::Item(item) => match item.kind {
211211
ItemKind::OpaqueTy(&hir::OpaqueTy {
212212
origin:
213-
hir::OpaqueTyOrigin::FnReturn { parent: fn_def_id }
214-
| hir::OpaqueTyOrigin::AsyncFn { parent: fn_def_id },
215-
in_trait,
213+
hir::OpaqueTyOrigin::FnReturn { parent: fn_def_id, in_trait_or_impl }
214+
| hir::OpaqueTyOrigin::AsyncFn { parent: fn_def_id, in_trait_or_impl },
216215
..
217216
}) => {
218-
if in_trait {
217+
if in_trait_or_impl.is_some() {
219218
assert_matches!(tcx.def_kind(fn_def_id), DefKind::AssocFn);
220219
} else {
221220
assert_matches!(tcx.def_kind(fn_def_id), DefKind::AssocFn | DefKind::Fn);

Diff for: compiler/rustc_hir_analysis/src/collect/item_bounds.rs

+39-31
Original file line numberDiff line numberDiff line change
@@ -370,39 +370,47 @@ pub(super) fn explicit_item_bounds_with_filter(
370370
..
371371
}) => associated_type_bounds(tcx, def_id, bounds, *span, filter),
372372
hir::Node::Item(hir::Item {
373-
kind: hir::ItemKind::OpaqueTy(hir::OpaqueTy { bounds, in_trait: false, .. }),
373+
kind: hir::ItemKind::OpaqueTy(hir::OpaqueTy { bounds, origin, .. }),
374374
span,
375375
..
376-
}) => {
377-
let args = GenericArgs::identity_for_item(tcx, def_id);
378-
let item_ty = Ty::new_opaque(tcx, def_id.to_def_id(), args);
379-
let bounds = opaque_type_bounds(tcx, def_id, bounds, item_ty, *span, filter);
380-
assert_only_contains_predicates_from(filter, bounds, item_ty);
381-
bounds
382-
}
383-
// Since RPITITs are lowered as projections in `<dyn HirTyLowerer>::lower_ty`, when we're
384-
// asking for the item bounds of the *opaques* in a trait's default method signature, we
385-
// need to map these projections back to opaques.
386-
hir::Node::Item(hir::Item {
387-
kind: hir::ItemKind::OpaqueTy(hir::OpaqueTy { bounds, in_trait: true, origin, .. }),
388-
span,
389-
..
390-
}) => {
391-
let (hir::OpaqueTyOrigin::FnReturn { parent: fn_def_id }
392-
| hir::OpaqueTyOrigin::AsyncFn { parent: fn_def_id }) = *origin
393-
else {
394-
span_bug!(*span, "RPITIT cannot be a TAIT, but got origin {origin:?}");
395-
};
396-
let args = GenericArgs::identity_for_item(tcx, def_id);
397-
let item_ty = Ty::new_opaque(tcx, def_id.to_def_id(), args);
398-
let bounds = &*tcx.arena.alloc_slice(
399-
&opaque_type_bounds(tcx, def_id, bounds, item_ty, *span, filter)
400-
.to_vec()
401-
.fold_with(&mut AssocTyToOpaque { tcx, fn_def_id: fn_def_id.to_def_id() }),
402-
);
403-
assert_only_contains_predicates_from(filter, bounds, item_ty);
404-
bounds
405-
}
376+
}) => match origin {
377+
// Since RPITITs are lowered as projections in `<dyn HirTyLowerer>::lower_ty`,
378+
// when we're asking for the item bounds of the *opaques* in a trait's default
379+
// method signature, we need to map these projections back to opaques.
380+
rustc_hir::OpaqueTyOrigin::FnReturn {
381+
parent,
382+
in_trait_or_impl: Some(hir::RpitContext::Trait),
383+
}
384+
| rustc_hir::OpaqueTyOrigin::AsyncFn {
385+
parent,
386+
in_trait_or_impl: Some(hir::RpitContext::Trait),
387+
} => {
388+
let args = GenericArgs::identity_for_item(tcx, def_id);
389+
let item_ty = Ty::new_opaque(tcx, def_id.to_def_id(), args);
390+
let bounds = &*tcx.arena.alloc_slice(
391+
&opaque_type_bounds(tcx, def_id, bounds, item_ty, *span, filter)
392+
.to_vec()
393+
.fold_with(&mut AssocTyToOpaque { tcx, fn_def_id: parent.to_def_id() }),
394+
);
395+
assert_only_contains_predicates_from(filter, bounds, item_ty);
396+
bounds
397+
}
398+
rustc_hir::OpaqueTyOrigin::FnReturn {
399+
parent: _,
400+
in_trait_or_impl: None | Some(hir::RpitContext::TraitImpl),
401+
}
402+
| rustc_hir::OpaqueTyOrigin::AsyncFn {
403+
parent: _,
404+
in_trait_or_impl: None | Some(hir::RpitContext::TraitImpl),
405+
}
406+
| rustc_hir::OpaqueTyOrigin::TyAlias { parent: _, .. } => {
407+
let args = GenericArgs::identity_for_item(tcx, def_id);
408+
let item_ty = Ty::new_opaque(tcx, def_id.to_def_id(), args);
409+
let bounds = opaque_type_bounds(tcx, def_id, bounds, item_ty, *span, filter);
410+
assert_only_contains_predicates_from(filter, bounds, item_ty);
411+
bounds
412+
}
413+
},
406414
hir::Node::Item(hir::Item { kind: hir::ItemKind::TyAlias(..), .. }) => &[],
407415
_ => bug!("item_bounds called on {:?}", def_id),
408416
};

Diff for: compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -515,8 +515,8 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
515515
}
516516
hir::ItemKind::OpaqueTy(&hir::OpaqueTy {
517517
origin:
518-
hir::OpaqueTyOrigin::FnReturn { parent }
519-
| hir::OpaqueTyOrigin::AsyncFn { parent }
518+
hir::OpaqueTyOrigin::FnReturn { parent, .. }
519+
| hir::OpaqueTyOrigin::AsyncFn { parent, .. }
520520
| hir::OpaqueTyOrigin::TyAlias { parent, .. },
521521
generics,
522522
..

Diff for: compiler/rustc_hir_analysis/src/collect/type_of.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -618,12 +618,13 @@ pub(super) fn type_of_opaque(
618618
// Opaque types desugared from `impl Trait`.
619619
ItemKind::OpaqueTy(&OpaqueTy {
620620
origin:
621-
hir::OpaqueTyOrigin::FnReturn { parent: owner }
622-
| hir::OpaqueTyOrigin::AsyncFn { parent: owner },
623-
in_trait,
621+
hir::OpaqueTyOrigin::FnReturn { parent: owner, in_trait_or_impl }
622+
| hir::OpaqueTyOrigin::AsyncFn { parent: owner, in_trait_or_impl },
624623
..
625624
}) => {
626-
if in_trait && !tcx.defaultness(owner).has_value() {
625+
if in_trait_or_impl == Some(hir::RpitContext::Trait)
626+
&& !tcx.defaultness(owner).has_value()
627+
{
627628
span_bug!(
628629
tcx.def_span(def_id),
629630
"tried to get type of this RPITIT with no definition"

0 commit comments

Comments
 (0)