Skip to content

Commit cd05fba

Browse files
authored
Unrolled build for rust-lang#131183
Rollup merge of rust-lang#131183 - compiler-errors:opaque-ty-origin, r=estebank Refactoring to `OpaqueTyOrigin` Pulled out of a larger PR that uses these changes to do cross-crate encoding of opaque origin, so we can use them for edition 2024 migrations. These changes should be self-explanatory on their own, tho 😄
2 parents 9ff5fc4 + 6e8573c commit cd05fba

File tree

34 files changed

+223
-163
lines changed

34 files changed

+223
-163
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

+41-46
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
@@ -1576,37 +1563,29 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15761563
lifetime_collector::lifetimes_in_bounds(self.resolver, bounds)
15771564
}
15781565
}
1579-
hir::OpaqueTyOrigin::AsyncFn(..) => {
1566+
hir::OpaqueTyOrigin::AsyncFn { .. } => {
15801567
unreachable!("should be using `lower_async_fn_ret_ty`")
15811568
}
15821569
}
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.
@@ -1776,7 +1753,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17761753
hir::TyKind::OpaqueDef(
17771754
hir::ItemId { owner_id: hir::OwnerId { def_id: opaque_ty_def_id } },
17781755
generic_args,
1779-
in_trait,
17801756
)
17811757
}
17821758

@@ -1864,12 +1840,23 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
18641840
None => match &decl.output {
18651841
FnRetTy::Ty(ty) => {
18661842
let itctx = match kind {
1867-
FnDeclKind::Fn
1868-
| FnDeclKind::Inherent
1869-
| FnDeclKind::Trait
1870-
| FnDeclKind::Impl => ImplTraitContext::OpaqueTy {
1871-
origin: hir::OpaqueTyOrigin::FnReturn(self.local_def_id(fn_node_id)),
1872-
fn_kind: Some(kind),
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 {
1850+
origin: hir::OpaqueTyOrigin::FnReturn {
1851+
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),
1859+
},
18731860
},
18741861
FnDeclKind::ExternFn => {
18751862
ImplTraitContext::Disallowed(ImplTraitPosition::ExternFnReturn)
@@ -1951,10 +1938,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19511938
.map(|(ident, id, _)| Lifetime { id, ident })
19521939
.collect();
19531940

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+
19541948
let opaque_ty_ref = self.lower_opaque_inner(
19551949
opaque_ty_node_id,
1956-
hir::OpaqueTyOrigin::AsyncFn(fn_def_id),
1957-
matches!(fn_kind, FnDeclKind::Trait),
1950+
hir::OpaqueTyOrigin::AsyncFn { parent: fn_def_id, in_trait_or_impl },
19581951
captured_lifetimes,
19591952
span,
19601953
opaque_ty_span,
@@ -1964,8 +1957,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19641957
coro,
19651958
opaque_ty_span,
19661959
ImplTraitContext::OpaqueTy {
1967-
origin: hir::OpaqueTyOrigin::FnReturn(fn_def_id),
1968-
fn_kind: Some(fn_kind),
1960+
origin: hir::OpaqueTyOrigin::FnReturn {
1961+
parent: fn_def_id,
1962+
in_trait_or_impl,
1963+
},
19691964
},
19701965
);
19711966
arena_vec![this; bound]

Diff for: compiler/rustc_borrowck/src/diagnostics/region_name.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
832832
fn get_future_inner_return_ty(&self, hir_ty: &'tcx hir::Ty<'tcx>) -> &'tcx hir::Ty<'tcx> {
833833
let hir = self.infcx.tcx.hir();
834834

835-
let hir::TyKind::OpaqueDef(id, _, _) = hir_ty.kind else {
835+
let hir::TyKind::OpaqueDef(id, _) = hir_ty.kind else {
836836
span_bug!(
837837
hir_ty.span,
838838
"lowered return type of async fn is not OpaqueDef: {:?}",

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

+20-8
Original file line numberDiff line numberDiff line change
@@ -2632,7 +2632,7 @@ impl<'hir> Ty<'hir> {
26322632
}
26332633
TyKind::Tup(tys) => tys.iter().any(Self::is_suggestable_infer_ty),
26342634
TyKind::Ptr(mut_ty) | TyKind::Ref(_, mut_ty) => mut_ty.ty.is_suggestable_infer_ty(),
2635-
TyKind::OpaqueDef(_, generic_args, _) => are_suggestable_generic_args(generic_args),
2635+
TyKind::OpaqueDef(_, generic_args) => are_suggestable_generic_args(generic_args),
26362636
TyKind::Path(QPath::TypeRelative(ty, segment)) => {
26372637
ty.is_suggestable_infer_ty() || are_suggestable_generic_args(segment.args().args)
26382638
}
@@ -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,13 +2798,29 @@ 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`
2809-
FnReturn(LocalDefId),
2811+
FnReturn {
2812+
/// The defining function.
2813+
parent: LocalDefId,
2814+
// Whether this is an RPITIT (return position impl trait in trait)
2815+
in_trait_or_impl: Option<RpitContext>,
2816+
},
28102817
/// `async fn`
2811-
AsyncFn(LocalDefId),
2818+
AsyncFn {
2819+
/// The defining function.
2820+
parent: LocalDefId,
2821+
// Whether this is an AFIT (async fn in trait)
2822+
in_trait_or_impl: Option<RpitContext>,
2823+
},
28122824
/// type aliases: `type Foo = impl Trait;`
28132825
TyAlias {
28142826
/// The type alias or associated type parent of the TAIT/ATPIT
@@ -2856,7 +2868,7 @@ pub enum TyKind<'hir> {
28562868
/// possibly parameters) that are actually bound on the `impl Trait`.
28572869
///
28582870
/// The last parameter specifies whether this opaque appears in a trait definition.
2859-
OpaqueDef(ItemId, &'hir [GenericArg<'hir>], bool),
2871+
OpaqueDef(ItemId, &'hir [GenericArg<'hir>]),
28602872
/// A trait object type `Bound1 + Bound2 + Bound3`
28612873
/// where `Bound` is a trait or a lifetime.
28622874
TraitObject(

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,7 @@ pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty<'v>) -> V::Resul
894894
TyKind::Path(ref qpath) => {
895895
try_visit!(visitor.visit_qpath(qpath, typ.hir_id, typ.span));
896896
}
897-
TyKind::OpaqueDef(item_id, lifetimes, _in_trait) => {
897+
TyKind::OpaqueDef(item_id, lifetimes) => {
898898
try_visit!(visitor.visit_nested_item(item_id));
899899
walk_list!(visitor, visit_generic_arg, lifetimes);
900900
}

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -336,18 +336,18 @@ fn check_opaque_meets_bounds<'tcx>(
336336
origin: &hir::OpaqueTyOrigin,
337337
) -> Result<(), ErrorGuaranteed> {
338338
let defining_use_anchor = match *origin {
339-
hir::OpaqueTyOrigin::FnReturn(did)
340-
| hir::OpaqueTyOrigin::AsyncFn(did)
341-
| hir::OpaqueTyOrigin::TyAlias { parent: did, .. } => did,
339+
hir::OpaqueTyOrigin::FnReturn { parent, .. }
340+
| hir::OpaqueTyOrigin::AsyncFn { parent, .. }
341+
| hir::OpaqueTyOrigin::TyAlias { parent, .. } => parent,
342342
};
343343
let param_env = tcx.param_env(defining_use_anchor);
344344

345345
let infcx = tcx.infer_ctxt().with_opaque_type_inference(defining_use_anchor).build();
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
)
@@ -409,7 +409,7 @@ fn check_opaque_meets_bounds<'tcx>(
409409
let outlives_env = OutlivesEnvironment::with_bounds(param_env, implied_bounds);
410410
ocx.resolve_regions_and_report_errors(defining_use_anchor, &outlives_env)?;
411411

412-
if let hir::OpaqueTyOrigin::FnReturn(..) | hir::OpaqueTyOrigin::AsyncFn(..) = origin {
412+
if let hir::OpaqueTyOrigin::FnReturn { .. } | hir::OpaqueTyOrigin::AsyncFn { .. } = origin {
413413
// HACK: this should also fall through to the hidden type check below, but the original
414414
// implementation had a bug where equivalent lifetimes are not identical. This caused us
415415
// to reject existing stable code that is otherwise completely fine. The real fix is to
@@ -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(fn_def_id)
740-
| hir::OpaqueTyOrigin::AsyncFn(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

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ 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(def_id) | hir::OpaqueTyOrigin::FnReturn(def_id)
98-
if def_id == impl_m.def_id.expect_local()
97+
hir::OpaqueTyOrigin::AsyncFn { parent, .. } | hir::OpaqueTyOrigin::FnReturn { parent, .. }
98+
if parent == impl_m.def_id.expect_local()
9999
)
100100
}) {
101101
report_mismatched_rpitit_signature(

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -210,11 +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(fn_def_id) | hir::OpaqueTyOrigin::AsyncFn(fn_def_id),
214-
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 },
215215
..
216216
}) => {
217-
if in_trait {
217+
if in_trait_or_impl.is_some() {
218218
assert_matches!(tcx.def_kind(fn_def_id), DefKind::AssocFn);
219219
} else {
220220
assert_matches!(tcx.def_kind(fn_def_id), DefKind::AssocFn | DefKind::Fn);

0 commit comments

Comments
 (0)