Skip to content

Commit ecbd707

Browse files
Move in_trait into OpaqueTyOrigin
1 parent cb7e369 commit ecbd707

File tree

19 files changed

+100
-123
lines changed

19 files changed

+100
-123
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

+27-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, .. } => {
1548+
if in_trait
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: true, .. } => {
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,17 @@ 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: false,
1847+
},
1848+
},
1849+
FnDeclKind::Trait | FnDeclKind::Impl => ImplTraitContext::OpaqueTy {
18701850
origin: hir::OpaqueTyOrigin::FnReturn {
18711851
parent: self.local_def_id(fn_node_id),
1852+
in_trait: true,
18721853
},
1873-
fn_kind: Some(kind),
18741854
},
18751855
FnDeclKind::ExternFn => {
18761856
ImplTraitContext::Disallowed(ImplTraitPosition::ExternFnReturn)
@@ -1952,10 +1932,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19521932
.map(|(ident, id, _)| Lifetime { id, ident })
19531933
.collect();
19541934

1935+
let in_trait = match fn_kind {
1936+
FnDeclKind::Trait | FnDeclKind::Impl => true,
1937+
FnDeclKind::Fn | FnDeclKind::Inherent => false,
1938+
FnDeclKind::ExternFn | FnDeclKind::Closure | FnDeclKind::Pointer => unreachable!(),
1939+
};
1940+
19551941
let opaque_ty_ref = self.lower_opaque_inner(
19561942
opaque_ty_node_id,
1957-
hir::OpaqueTyOrigin::AsyncFn { parent: fn_def_id },
1958-
matches!(fn_kind, FnDeclKind::Trait),
1943+
hir::OpaqueTyOrigin::AsyncFn { parent: fn_def_id, in_trait },
19591944
captured_lifetimes,
19601945
span,
19611946
opaque_ty_span,
@@ -1965,8 +1950,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19651950
coro,
19661951
opaque_ty_span,
19671952
ImplTraitContext::OpaqueTy {
1968-
origin: hir::OpaqueTyOrigin::FnReturn { parent: fn_def_id },
1969-
fn_kind: Some(fn_kind),
1953+
origin: hir::OpaqueTyOrigin::FnReturn { parent: fn_def_id, in_trait },
19701954
},
19711955
);
19721956
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

+4-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)]
@@ -2809,11 +2805,15 @@ pub enum OpaqueTyOrigin {
28092805
FnReturn {
28102806
/// The defining function.
28112807
parent: LocalDefId,
2808+
// Whether this is an RPITIT (return position impl trait in trait)
2809+
in_trait: bool,
28122810
},
28132811
/// `async fn`
28142812
AsyncFn {
28152813
/// The defining function.
28162814
parent: LocalDefId,
2815+
// Whether this is an AFIT (async fn in trait)
2816+
in_trait: bool,
28172817
},
28182818
/// type aliases: `type Foo = impl Trait;`
28192819
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

+2-3
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,8 @@ 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 }
214+
| hir::OpaqueTyOrigin::AsyncFn { parent: fn_def_id, in_trait },
216215
..
217216
}) => {
218217
if in_trait {

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

+27-31
Original file line numberDiff line numberDiff line change
@@ -370,39 +370,35 @@ 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 { parent, in_trait: true }
381+
| rustc_hir::OpaqueTyOrigin::AsyncFn { parent, in_trait: true } => {
382+
let args = GenericArgs::identity_for_item(tcx, def_id);
383+
let item_ty = Ty::new_opaque(tcx, def_id.to_def_id(), args);
384+
let bounds = &*tcx.arena.alloc_slice(
385+
&opaque_type_bounds(tcx, def_id, bounds, item_ty, *span, filter)
386+
.to_vec()
387+
.fold_with(&mut AssocTyToOpaque { tcx, fn_def_id: parent.to_def_id() }),
388+
);
389+
assert_only_contains_predicates_from(filter, bounds, item_ty);
390+
bounds
391+
}
392+
rustc_hir::OpaqueTyOrigin::FnReturn { parent: _, in_trait: false }
393+
| rustc_hir::OpaqueTyOrigin::AsyncFn { parent: _, in_trait: false }
394+
| rustc_hir::OpaqueTyOrigin::TyAlias { parent: _, .. } => {
395+
let args = GenericArgs::identity_for_item(tcx, def_id);
396+
let item_ty = Ty::new_opaque(tcx, def_id.to_def_id(), args);
397+
let bounds = opaque_type_bounds(tcx, def_id, bounds, item_ty, *span, filter);
398+
assert_only_contains_predicates_from(filter, bounds, item_ty);
399+
bounds
400+
}
401+
},
406402
hir::Node::Item(hir::Item { kind: hir::ItemKind::TyAlias(..), .. }) => &[],
407403
_ => bug!("item_bounds called on {:?}", def_id),
408404
};

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -618,9 +618,8 @@ 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 }
622+
| hir::OpaqueTyOrigin::AsyncFn { parent: owner, in_trait },
624623
..
625624
}) => {
626625
if in_trait && !tcx.defaultness(owner).has_value() {

Diff for: compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+16-7
Original file line numberDiff line numberDiff line change
@@ -2091,17 +2091,26 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
20912091
let opaque_ty = tcx.hir().item(item_id);
20922092

20932093
match opaque_ty.kind {
2094-
hir::ItemKind::OpaqueTy(&hir::OpaqueTy { in_trait, .. }) => {
2094+
hir::ItemKind::OpaqueTy(&hir::OpaqueTy { origin, .. }) => {
20952095
let local_def_id = item_id.owner_id.def_id;
20962096
// If this is an RPITIT and we are using the new RPITIT lowering scheme, we
20972097
// generate the def_id of an associated type for the trait and return as
20982098
// type a projection.
2099-
let def_id = if in_trait {
2100-
tcx.associated_type_for_impl_trait_in_trait(local_def_id).to_def_id()
2101-
} else {
2102-
local_def_id.to_def_id()
2103-
};
2104-
self.lower_opaque_ty(def_id, lifetimes, in_trait)
2099+
match origin {
2100+
hir::OpaqueTyOrigin::FnReturn { in_trait: true, .. }
2101+
| hir::OpaqueTyOrigin::AsyncFn { in_trait: true, .. } => self
2102+
.lower_opaque_ty(
2103+
tcx.associated_type_for_impl_trait_in_trait(local_def_id)
2104+
.to_def_id(),
2105+
lifetimes,
2106+
true,
2107+
),
2108+
hir::OpaqueTyOrigin::FnReturn { in_trait: false, .. }
2109+
| hir::OpaqueTyOrigin::AsyncFn { in_trait: false, .. }
2110+
| hir::OpaqueTyOrigin::TyAlias { .. } => {
2111+
self.lower_opaque_ty(local_def_id.to_def_id(), lifetimes, false)
2112+
}
2113+
}
21052114
}
21062115
ref i => bug!("`impl Trait` pointed to non-opaque type?? {:#?}", i),
21072116
}

Diff for: compiler/rustc_hir_typeck/src/_match.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
602602
.map(|(k, _)| (k.def_id, k.args))?,
603603
_ => return None,
604604
};
605-
let hir::OpaqueTyOrigin::FnReturn { parent: parent_def_id } =
605+
let hir::OpaqueTyOrigin::FnReturn { parent: parent_def_id, .. } =
606606
self.tcx.opaque_type_origin(def_id)
607607
else {
608608
return None;

0 commit comments

Comments
 (0)