Skip to content

Commit 72d4713

Browse files
committed
Store impl_trait_fn inside OpaqueTyOrigin.
1 parent 8e1b87b commit 72d4713

File tree

13 files changed

+121
-94
lines changed

13 files changed

+121
-94
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
@@ -228,7 +228,7 @@ enum ImplTraitContext<'b, 'a> {
228228
ReturnPositionOpaqueTy {
229229
/// `DefId` for the parent function, used to look up necessary
230230
/// information later.
231-
fn_def_id: DefId,
231+
fn_def_id: LocalDefId,
232232
/// Origin: Either OpaqueTyOrigin::FnReturn or OpaqueTyOrigin::AsyncFn,
233233
origin: hir::OpaqueTyOrigin,
234234
},
@@ -1376,7 +1376,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13761376
fn lower_opaque_impl_trait(
13771377
&mut self,
13781378
span: Span,
1379-
fn_def_id: Option<DefId>,
1379+
fn_def_id: Option<LocalDefId>,
13801380
origin: hir::OpaqueTyOrigin,
13811381
opaque_ty_node_id: NodeId,
13821382
capturable_lifetimes: Option<&FxHashSet<hir::LifetimeName>>,
@@ -1448,7 +1448,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14481448
span: lctx.lower_span(span),
14491449
},
14501450
bounds: hir_bounds,
1451-
impl_trait_fn: fn_def_id,
14521451
origin,
14531452
};
14541453

@@ -1518,7 +1517,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15181517
fn lower_fn_decl(
15191518
&mut self,
15201519
decl: &FnDecl,
1521-
mut in_band_ty_params: Option<(DefId, &mut Vec<hir::GenericParam<'hir>>)>,
1520+
mut in_band_ty_params: Option<(LocalDefId, &mut Vec<hir::GenericParam<'hir>>)>,
15221521
impl_trait_return_allow: bool,
15231522
make_ret_async: Option<NodeId>,
15241523
) -> &'hir hir::FnDecl<'hir> {
@@ -1576,7 +1575,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15761575
Some((def_id, _)) if impl_trait_return_allow => {
15771576
ImplTraitContext::ReturnPositionOpaqueTy {
15781577
fn_def_id: def_id,
1579-
origin: hir::OpaqueTyOrigin::FnReturn,
1578+
origin: hir::OpaqueTyOrigin::FnReturn(def_id),
15801579
}
15811580
}
15821581
_ => ImplTraitContext::disallowed(),
@@ -1631,7 +1630,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
16311630
fn lower_async_fn_ret_ty(
16321631
&mut self,
16331632
output: &FnRetTy,
1634-
fn_def_id: DefId,
1633+
fn_def_id: LocalDefId,
16351634
opaque_ty_node_id: NodeId,
16361635
) -> hir::FnRetTy<'hir> {
16371636
debug!(
@@ -1746,8 +1745,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17461745
span: this.lower_span(span),
17471746
},
17481747
bounds: arena_vec![this; future_bound],
1749-
impl_trait_fn: Some(fn_def_id),
1750-
origin: hir::OpaqueTyOrigin::AsyncFn,
1748+
origin: hir::OpaqueTyOrigin::AsyncFn(fn_def_id),
17511749
};
17521750

17531751
trace!("exist ty from async fn def id: {:#?}", opaque_ty_def_id);
@@ -1793,7 +1791,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17931791
fn lower_async_fn_output_type_to_future_bound(
17941792
&mut self,
17951793
output: &FnRetTy,
1796-
fn_def_id: DefId,
1794+
fn_def_id: LocalDefId,
17971795
span: Span,
17981796
) -> hir::GenericBound<'hir> {
17991797
// Compute the `T` in `Future<Output = T>` from the return type.
@@ -1804,7 +1802,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
18041802
// generates.
18051803
let context = ImplTraitContext::ReturnPositionOpaqueTy {
18061804
fn_def_id,
1807-
origin: hir::OpaqueTyOrigin::FnReturn,
1805+
origin: hir::OpaqueTyOrigin::FnReturn(fn_def_id),
18081806
};
18091807
self.lower_ty(ty, context)
18101808
}
@@ -2440,17 +2438,12 @@ impl<'hir> GenericArgsCtor<'hir> {
24402438
}
24412439
}
24422440

2441+
#[tracing::instrument(level = "debug")]
24432442
fn lifetimes_from_impl_trait_bounds(
24442443
opaque_ty_id: NodeId,
24452444
bounds: hir::GenericBounds<'_>,
24462445
lifetimes_to_include: Option<&FxHashSet<hir::LifetimeName>>,
24472446
) -> Vec<(hir::LifetimeName, Span)> {
2448-
debug!(
2449-
"lifetimes_from_impl_trait_bounds(opaque_ty_id={:?}, \
2450-
bounds={:#?})",
2451-
opaque_ty_id, bounds,
2452-
);
2453-
24542447
// This visitor walks over `impl Trait` bounds and creates defs for all lifetimes that
24552448
// appear in the bounds, excluding lifetimes that are created within the bounds.
24562449
// 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
@@ -2239,17 +2239,16 @@ pub struct BareFnTy<'hir> {
22392239
pub struct OpaqueTy<'hir> {
22402240
pub generics: Generics<'hir>,
22412241
pub bounds: GenericBounds<'hir>,
2242-
pub impl_trait_fn: Option<DefId>,
22432242
pub origin: OpaqueTyOrigin,
22442243
}
22452244

22462245
/// From whence the opaque type came.
22472246
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Decodable, Debug, HashStable_Generic)]
22482247
pub enum OpaqueTyOrigin {
22492248
/// `-> impl Trait`
2250-
FnReturn,
2249+
FnReturn(LocalDefId),
22512250
/// `async fn`
2252-
AsyncFn,
2251+
AsyncFn(LocalDefId),
22532252
/// type aliases: `type Foo = impl Trait;`
22542253
TyAlias,
22552254
}
@@ -2800,7 +2799,9 @@ impl ItemKind<'_> {
28002799
Some(match *self {
28012800
ItemKind::Fn(_, ref generics, _)
28022801
| ItemKind::TyAlias(_, ref generics)
2803-
| ItemKind::OpaqueTy(OpaqueTy { ref generics, impl_trait_fn: None, .. })
2802+
| ItemKind::OpaqueTy(OpaqueTy {
2803+
ref generics, origin: OpaqueTyOrigin::TyAlias, ..
2804+
})
28042805
| ItemKind::Enum(_, ref generics)
28052806
| ItemKind::Struct(_, ref generics)
28062807
| 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

+16-7
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>
@@ -468,20 +468,29 @@ impl<'a, 'tcx> Instantiator<'a, 'tcx> {
468468
};
469469
let (in_definition_scope, origin) =
470470
match tcx.hir().expect_item(def_id).kind {
471+
// Async `impl Trait`
472+
hir::ItemKind::OpaqueTy(hir::OpaqueTy {
473+
origin: hir::OpaqueTyOrigin::AsyncFn(parent),
474+
..
475+
}) => (
476+
parent == parent_def_id,
477+
hir::OpaqueTyOrigin::AsyncFn(parent),
478+
),
471479
// Anonymous `impl Trait`
472480
hir::ItemKind::OpaqueTy(hir::OpaqueTy {
473-
impl_trait_fn: Some(parent),
474-
origin,
481+
origin: hir::OpaqueTyOrigin::FnReturn(parent),
475482
..
476-
}) => (parent == parent_def_id.to_def_id(), origin),
483+
}) => (
484+
parent == parent_def_id,
485+
hir::OpaqueTyOrigin::FnReturn(parent),
486+
),
477487
// Named `type Foo = impl Bar;`
478488
hir::ItemKind::OpaqueTy(hir::OpaqueTy {
479-
impl_trait_fn: None,
480-
origin,
489+
origin: hir::OpaqueTyOrigin::TyAlias,
481490
..
482491
}) => (
483492
may_define_opaque_type(tcx, parent_def_id, opaque_hir_id),
484-
origin,
493+
hir::OpaqueTyOrigin::TyAlias,
485494
),
486495
_ => (def_scope_default(), hir::OpaqueTyOrigin::TyAlias),
487496
};

compiler/rustc_middle/src/ty/mod.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -2052,13 +2052,17 @@ impl<'tcx> TyCtxt<'tcx> {
20522052
}
20532053
}
20542054

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