Skip to content

Commit 2c506db

Browse files
committed
Store impl_trait_fn inside OpaqueTyOrigin.
1 parent 2c0b515 commit 2c506db

File tree

13 files changed

+118
-96
lines changed

13 files changed

+118
-96
lines changed

compiler/rustc_ast_lowering/src/item.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
247247
AnonymousLifetimeMode::PassThrough,
248248
|this, idty| {
249249
let ret_id = asyncness.opt_return_id();
250-
this.lower_fn_decl(
251-
&decl,
252-
Some((fn_def_id.to_def_id(), idty)),
253-
true,
254-
ret_id,
255-
)
250+
this.lower_fn_decl(&decl, Some((fn_def_id, idty)), true, ret_id)
256251
},
257252
);
258253
let sig = hir::FnSig {
@@ -1264,7 +1259,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
12641259
|this, idty| {
12651260
this.lower_fn_decl(
12661261
&sig.decl,
1267-
Some((fn_def_id.to_def_id(), idty)),
1262+
Some((fn_def_id, idty)),
12681263
impl_trait_return_allow,
12691264
is_async,
12701265
)

compiler/rustc_ast_lowering/src/lib.rs

+9-16
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ enum ImplTraitContext<'b, 'a> {
229229
ReturnPositionOpaqueTy {
230230
/// `DefId` for the parent function, used to look up necessary
231231
/// information later.
232-
fn_def_id: DefId,
232+
fn_def_id: LocalDefId,
233233
/// Origin: Either OpaqueTyOrigin::FnReturn or OpaqueTyOrigin::AsyncFn,
234234
origin: hir::OpaqueTyOrigin,
235235
},
@@ -1378,7 +1378,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13781378
fn lower_opaque_impl_trait(
13791379
&mut self,
13801380
span: Span,
1381-
fn_def_id: Option<DefId>,
1381+
fn_def_id: Option<LocalDefId>,
13821382
origin: hir::OpaqueTyOrigin,
13831383
opaque_ty_node_id: NodeId,
13841384
capturable_lifetimes: Option<&FxHashSet<hir::LifetimeName>>,
@@ -1450,7 +1450,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14501450
span: lctx.lower_span(span),
14511451
},
14521452
bounds: hir_bounds,
1453-
impl_trait_fn: fn_def_id,
14541453
origin,
14551454
};
14561455

@@ -1520,7 +1519,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15201519
fn lower_fn_decl(
15211520
&mut self,
15221521
decl: &FnDecl,
1523-
mut in_band_ty_params: Option<(DefId, &mut Vec<hir::GenericParam<'hir>>)>,
1522+
mut in_band_ty_params: Option<(LocalDefId, &mut Vec<hir::GenericParam<'hir>>)>,
15241523
impl_trait_return_allow: bool,
15251524
make_ret_async: Option<NodeId>,
15261525
) -> &'hir hir::FnDecl<'hir> {
@@ -1578,7 +1577,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15781577
Some((def_id, _)) if impl_trait_return_allow => {
15791578
ImplTraitContext::ReturnPositionOpaqueTy {
15801579
fn_def_id: def_id,
1581-
origin: hir::OpaqueTyOrigin::FnReturn,
1580+
origin: hir::OpaqueTyOrigin::FnReturn(def_id),
15821581
}
15831582
}
15841583
_ => ImplTraitContext::disallowed(),
@@ -1633,7 +1632,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
16331632
fn lower_async_fn_ret_ty(
16341633
&mut self,
16351634
output: &FnRetTy,
1636-
fn_def_id: DefId,
1635+
fn_def_id: LocalDefId,
16371636
opaque_ty_node_id: NodeId,
16381637
) -> hir::FnRetTy<'hir> {
16391638
debug!(
@@ -1748,8 +1747,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17481747
span: this.lower_span(span),
17491748
},
17501749
bounds: arena_vec![this; future_bound],
1751-
impl_trait_fn: Some(fn_def_id),
1752-
origin: hir::OpaqueTyOrigin::AsyncFn,
1750+
origin: hir::OpaqueTyOrigin::AsyncFn(fn_def_id),
17531751
};
17541752

17551753
trace!("exist ty from async fn def id: {:#?}", opaque_ty_def_id);
@@ -1795,7 +1793,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17951793
fn lower_async_fn_output_type_to_future_bound(
17961794
&mut self,
17971795
output: &FnRetTy,
1798-
fn_def_id: DefId,
1796+
fn_def_id: LocalDefId,
17991797
span: Span,
18001798
) -> hir::GenericBound<'hir> {
18011799
// Compute the `T` in `Future<Output = T>` from the return type.
@@ -1806,7 +1804,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
18061804
// generates.
18071805
let context = ImplTraitContext::ReturnPositionOpaqueTy {
18081806
fn_def_id,
1809-
origin: hir::OpaqueTyOrigin::FnReturn,
1807+
origin: hir::OpaqueTyOrigin::FnReturn(fn_def_id),
18101808
};
18111809
self.lower_ty(ty, context)
18121810
}
@@ -2443,17 +2441,12 @@ impl<'hir> GenericArgsCtor<'hir> {
24432441
}
24442442
}
24452443

2444+
#[tracing::instrument(level = "debug")]
24462445
fn lifetimes_from_impl_trait_bounds(
24472446
opaque_ty_id: NodeId,
24482447
bounds: hir::GenericBounds<'_>,
24492448
lifetimes_to_include: Option<&FxHashSet<hir::LifetimeName>>,
24502449
) -> Vec<(hir::LifetimeName, Span)> {
2451-
debug!(
2452-
"lifetimes_from_impl_trait_bounds(opaque_ty_id={:?}, \
2453-
bounds={:#?})",
2454-
opaque_ty_id, bounds,
2455-
);
2456-
24572450
// This visitor walks over `impl Trait` bounds and creates defs for all lifetimes that
24582451
// appear in the bounds, excluding lifetimes that are created within the bounds.
24592452
// E.g., `'a`, `'b`, but not `'c` in `impl for<'c> SomeTrait<'a, 'b, 'c>`.

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ fn check_opaque_type_parameter_valid(
173173
// fn foo<l0..'ln>() -> foo::<'static..'static>::Foo<'l0..'lm>.
174174
//
175175
// which would error here on all of the `'static` args.
176-
OpaqueTyOrigin::FnReturn | OpaqueTyOrigin::AsyncFn => return true,
176+
OpaqueTyOrigin::FnReturn(..) | OpaqueTyOrigin::AsyncFn(..) => return true,
177177
// Check these
178178
OpaqueTyOrigin::TyAlias => {}
179179
}

compiler/rustc_hir/src/hir.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -2241,17 +2241,16 @@ pub struct BareFnTy<'hir> {
22412241
pub struct OpaqueTy<'hir> {
22422242
pub generics: Generics<'hir>,
22432243
pub bounds: GenericBounds<'hir>,
2244-
pub impl_trait_fn: Option<DefId>,
22452244
pub origin: OpaqueTyOrigin,
22462245
}
22472246

22482247
/// From whence the opaque type came.
22492248
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Decodable, Debug, HashStable_Generic)]
22502249
pub enum OpaqueTyOrigin {
22512250
/// `-> impl Trait`
2252-
FnReturn,
2251+
FnReturn(LocalDefId),
22532252
/// `async fn`
2254-
AsyncFn,
2253+
AsyncFn(LocalDefId),
22552254
/// type aliases: `type Foo = impl Trait;`
22562255
TyAlias,
22572256
}
@@ -2802,7 +2801,9 @@ impl ItemKind<'_> {
28022801
Some(match *self {
28032802
ItemKind::Fn(_, ref generics, _)
28042803
| ItemKind::TyAlias(_, ref generics)
2805-
| ItemKind::OpaqueTy(OpaqueTy { ref generics, impl_trait_fn: None, .. })
2804+
| ItemKind::OpaqueTy(OpaqueTy {
2805+
ref generics, origin: OpaqueTyOrigin::TyAlias, ..
2806+
})
28062807
| ItemKind::Enum(_, ref generics)
28072808
| ItemKind::Struct(_, ref generics)
28082809
| ItemKind::Union(_, ref generics)

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
107107
kind:
108108
hir::ItemKind::OpaqueTy(hir::OpaqueTy {
109109
bounds,
110-
origin: hir::OpaqueTyOrigin::AsyncFn,
110+
origin: hir::OpaqueTyOrigin::AsyncFn(..),
111111
..
112112
}),
113113
..

compiler/rustc_infer/src/infer/opaque_types.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
276276
debug!(?concrete_ty);
277277

278278
let first_own_region = match opaque_defn.origin {
279-
hir::OpaqueTyOrigin::FnReturn | hir::OpaqueTyOrigin::AsyncFn => {
279+
hir::OpaqueTyOrigin::FnReturn(..) | hir::OpaqueTyOrigin::AsyncFn(..) => {
280280
// We lower
281281
//
282282
// fn foo<'l0..'ln>() -> impl Trait<'l0..'lm>
@@ -467,20 +467,24 @@ impl<'a, 'tcx> Instantiator<'a, 'tcx> {
467467
};
468468
let (in_definition_scope, origin) = match tcx.hir().expect_item(def_id).kind
469469
{
470+
// Async `impl Trait`
471+
hir::ItemKind::OpaqueTy(hir::OpaqueTy {
472+
origin: hir::OpaqueTyOrigin::AsyncFn(parent),
473+
..
474+
}) => (parent == parent_def_id, hir::OpaqueTyOrigin::AsyncFn(parent)),
470475
// Anonymous `impl Trait`
471476
hir::ItemKind::OpaqueTy(hir::OpaqueTy {
472-
impl_trait_fn: Some(parent),
473-
origin,
477+
origin: hir::OpaqueTyOrigin::FnReturn(parent),
474478
..
475-
}) => (parent == parent_def_id.to_def_id(), origin),
479+
}) => (parent == parent_def_id, hir::OpaqueTyOrigin::FnReturn(parent)),
476480
// Named `type Foo = impl Bar;`
477481
hir::ItemKind::OpaqueTy(hir::OpaqueTy {
478-
impl_trait_fn: None,
479-
origin,
482+
origin: hir::OpaqueTyOrigin::TyAlias,
480483
..
481-
}) => {
482-
(may_define_opaque_type(tcx, parent_def_id, opaque_hir_id), origin)
483-
}
484+
}) => (
485+
may_define_opaque_type(tcx, parent_def_id, opaque_hir_id),
486+
hir::OpaqueTyOrigin::TyAlias,
487+
),
484488
_ => (def_scope_default(), hir::OpaqueTyOrigin::TyAlias),
485489
};
486490
if in_definition_scope {

compiler/rustc_middle/src/ty/mod.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -2055,13 +2055,17 @@ impl<'tcx> TyCtxt<'tcx> {
20552055
}
20562056
}
20572057

2058-
/// Yields the parent function's `DefId` if `def_id` is an `impl Trait` definition.
2059-
pub fn is_impl_trait_defn(tcx: TyCtxt<'_>, def_id: DefId) -> Option<DefId> {
2060-
if let Some(def_id) = def_id.as_local() {
2061-
if let Node::Item(item) = tcx.hir().get(tcx.hir().local_def_id_to_hir_id(def_id)) {
2062-
if let hir::ItemKind::OpaqueTy(ref opaque_ty) = item.kind {
2063-
return opaque_ty.impl_trait_fn;
2064-
}
2058+
/// Yields the parent function's `LocalDefId` if `def_id` is an `impl Trait` definition.
2059+
pub fn is_impl_trait_defn(tcx: TyCtxt<'_>, def_id: DefId) -> Option<LocalDefId> {
2060+
let def_id = def_id.as_local()?;
2061+
if let Node::Item(item) = tcx.hir().get(tcx.hir().local_def_id_to_hir_id(def_id)) {
2062+
if let hir::ItemKind::OpaqueTy(ref opaque_ty) = item.kind {
2063+
return match opaque_ty.origin {
2064+
hir::OpaqueTyOrigin::FnReturn(parent) | hir::OpaqueTyOrigin::AsyncFn(parent) => {
2065+
Some(parent)
2066+
}
2067+
hir::OpaqueTyOrigin::TyAlias => None,
2068+
};
20652069
}
20662070
}
20672071
None

compiler/rustc_resolve/src/late/lifetimes.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -968,7 +968,10 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
968968
let (generics, bounds) = match opaque_ty.kind {
969969
// Named opaque `impl Trait` types are reached via `TyKind::Path`.
970970
// This arm is for `impl Trait` in the types of statics, constants and locals.
971-
hir::ItemKind::OpaqueTy(hir::OpaqueTy { impl_trait_fn: None, .. }) => {
971+
hir::ItemKind::OpaqueTy(hir::OpaqueTy {
972+
origin: hir::OpaqueTyOrigin::TyAlias,
973+
..
974+
}) => {
972975
intravisit::walk_ty(self, ty);
973976

974977
// Elided lifetimes are not allowed in non-return
@@ -985,7 +988,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
985988
}
986989
// RPIT (return position impl trait)
987990
hir::ItemKind::OpaqueTy(hir::OpaqueTy {
988-
impl_trait_fn: Some(_),
991+
origin: hir::OpaqueTyOrigin::FnReturn(..) | hir::OpaqueTyOrigin::AsyncFn(..),
989992
ref generics,
990993
bounds,
991994
..
@@ -1695,7 +1698,11 @@ fn compute_object_lifetime_defaults(
16951698
hir::ItemKind::Struct(_, ref generics)
16961699
| hir::ItemKind::Union(_, ref generics)
16971700
| hir::ItemKind::Enum(_, ref generics)
1698-
| hir::ItemKind::OpaqueTy(hir::OpaqueTy { ref generics, impl_trait_fn: None, .. })
1701+
| hir::ItemKind::OpaqueTy(hir::OpaqueTy {
1702+
ref generics,
1703+
origin: hir::OpaqueTyOrigin::TyAlias,
1704+
..
1705+
})
16991706
| hir::ItemKind::TyAlias(_, ref generics)
17001707
| hir::ItemKind::Trait(_, _, ref generics, ..) => {
17011708
let result = object_lifetime_defaults_for_item(tcx, generics);
@@ -2067,7 +2074,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
20672074
..
20682075
}) = self.tcx.hir().get(parent_hir_id)
20692076
{
2070-
if opaque.origin != hir::OpaqueTyOrigin::AsyncFn {
2077+
if !matches!(opaque.origin, hir::OpaqueTyOrigin::AsyncFn(..)) {
20712078
continue 'lifetimes;
20722079
}
20732080
// We want to do this only if the liftime identifier is already defined

compiler/rustc_ty_utils/src/ty.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ fn trait_of_item(tcx: TyCtxt<'_>, def_id: DefId) -> Option<DefId> {
248248
fn param_env(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ParamEnv<'_> {
249249
// The param_env of an impl Trait type is its defining function's param_env
250250
if let Some(parent) = ty::is_impl_trait_defn(tcx, def_id) {
251-
return param_env(tcx, parent);
251+
return param_env(tcx, parent.to_def_id());
252252
}
253253
// Compute the bounds on Self and the type parameters.
254254

@@ -313,7 +313,7 @@ fn well_formed_types_in_env<'tcx>(
313313

314314
// The environment of an impl Trait type is its defining function's environment.
315315
if let Some(parent) = ty::is_impl_trait_defn(tcx, def_id) {
316-
return well_formed_types_in_env(tcx, parent);
316+
return well_formed_types_in_env(tcx, parent.to_def_id());
317317
}
318318

319319
// Compute the bounds on `Self` and the type parameters.

compiler/rustc_typeck/src/astconv/mod.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -2337,9 +2337,16 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
23372337
let def_id = item_id.def_id.to_def_id();
23382338

23392339
match opaque_ty.kind {
2340-
hir::ItemKind::OpaqueTy(hir::OpaqueTy { impl_trait_fn, .. }) => {
2341-
self.impl_trait_ty_to_ty(def_id, lifetimes, impl_trait_fn.is_some())
2342-
}
2340+
hir::ItemKind::OpaqueTy(hir::OpaqueTy { origin, .. }) => self
2341+
.impl_trait_ty_to_ty(
2342+
def_id,
2343+
lifetimes,
2344+
matches!(
2345+
origin,
2346+
hir::OpaqueTyOrigin::FnReturn(..)
2347+
| hir::OpaqueTyOrigin::AsyncFn(..)
2348+
),
2349+
),
23432350
ref i => bug!("`impl Trait` pointed to non-opaque type?? {:#?}", i),
23442351
}
23452352
}

compiler/rustc_typeck/src/check/check.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ pub(super) fn check_opaque_for_inheriting_lifetimes(
541541
}
542542

543543
if let ItemKind::OpaqueTy(hir::OpaqueTy {
544-
origin: hir::OpaqueTyOrigin::AsyncFn | hir::OpaqueTyOrigin::FnReturn,
544+
origin: hir::OpaqueTyOrigin::AsyncFn(..) | hir::OpaqueTyOrigin::FnReturn(..),
545545
..
546546
}) = item.kind
547547
{
@@ -567,7 +567,7 @@ pub(super) fn check_opaque_for_inheriting_lifetimes(
567567
visitor.visit_item(&item);
568568
let is_async = match item.kind {
569569
ItemKind::OpaqueTy(hir::OpaqueTy { origin, .. }) => {
570-
matches!(origin, hir::OpaqueTyOrigin::AsyncFn)
570+
matches!(origin, hir::OpaqueTyOrigin::AsyncFn(..))
571571
}
572572
_ => unreachable!(),
573573
};
@@ -604,7 +604,7 @@ pub(super) fn check_opaque_for_cycles<'tcx>(
604604
) -> Result<(), ErrorReported> {
605605
if tcx.try_expand_impl_trait_type(def_id.to_def_id(), substs).is_err() {
606606
match origin {
607-
hir::OpaqueTyOrigin::AsyncFn => async_opaque_type_cycle_error(tcx, span),
607+
hir::OpaqueTyOrigin::AsyncFn(..) => async_opaque_type_cycle_error(tcx, span),
608608
_ => opaque_type_cycle_error(tcx, def_id, span),
609609
}
610610
Err(ErrorReported)
@@ -635,7 +635,7 @@ fn check_opaque_meets_bounds<'tcx>(
635635
) {
636636
match origin {
637637
// Checked when type checking the function containing them.
638-
hir::OpaqueTyOrigin::FnReturn | hir::OpaqueTyOrigin::AsyncFn => return,
638+
hir::OpaqueTyOrigin::FnReturn(..) | hir::OpaqueTyOrigin::AsyncFn(..) => return,
639639
// Can have different predicates to their defining use
640640
hir::OpaqueTyOrigin::TyAlias => {}
641641
}

0 commit comments

Comments
 (0)