Skip to content

Commit 8576ab4

Browse files
committed
Store impl_trait_fn inside OpaqueTyOrigin.
1 parent 72b6f70 commit 8576ab4

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
@@ -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
},
@@ -1377,7 +1377,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13771377
fn lower_opaque_impl_trait(
13781378
&mut self,
13791379
span: Span,
1380-
fn_def_id: Option<DefId>,
1380+
fn_def_id: Option<LocalDefId>,
13811381
origin: hir::OpaqueTyOrigin,
13821382
opaque_ty_node_id: NodeId,
13831383
capturable_lifetimes: Option<&FxHashSet<hir::LifetimeName>>,
@@ -1449,7 +1449,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14491449
span: lctx.lower_span(span),
14501450
},
14511451
bounds: hir_bounds,
1452-
impl_trait_fn: fn_def_id,
14531452
origin,
14541453
};
14551454

@@ -1519,7 +1518,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15191518
fn lower_fn_decl(
15201519
&mut self,
15211520
decl: &FnDecl,
1522-
mut in_band_ty_params: Option<(DefId, &mut Vec<hir::GenericParam<'hir>>)>,
1521+
mut in_band_ty_params: Option<(LocalDefId, &mut Vec<hir::GenericParam<'hir>>)>,
15231522
impl_trait_return_allow: bool,
15241523
make_ret_async: Option<NodeId>,
15251524
) -> &'hir hir::FnDecl<'hir> {
@@ -1577,7 +1576,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15771576
Some((def_id, _)) if impl_trait_return_allow => {
15781577
ImplTraitContext::ReturnPositionOpaqueTy {
15791578
fn_def_id: def_id,
1580-
origin: hir::OpaqueTyOrigin::FnReturn,
1579+
origin: hir::OpaqueTyOrigin::FnReturn(def_id),
15811580
}
15821581
}
15831582
_ => ImplTraitContext::disallowed(),
@@ -1632,7 +1631,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
16321631
fn lower_async_fn_ret_ty(
16331632
&mut self,
16341633
output: &FnRetTy,
1635-
fn_def_id: DefId,
1634+
fn_def_id: LocalDefId,
16361635
opaque_ty_node_id: NodeId,
16371636
) -> hir::FnRetTy<'hir> {
16381637
debug!(
@@ -1747,8 +1746,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17471746
span: this.lower_span(span),
17481747
},
17491748
bounds: arena_vec![this; future_bound],
1750-
impl_trait_fn: Some(fn_def_id),
1751-
origin: hir::OpaqueTyOrigin::AsyncFn,
1749+
origin: hir::OpaqueTyOrigin::AsyncFn(fn_def_id),
17521750
};
17531751

17541752
trace!("exist ty from async fn def id: {:#?}", opaque_ty_def_id);
@@ -1794,7 +1792,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17941792
fn lower_async_fn_output_type_to_future_bound(
17951793
&mut self,
17961794
output: &FnRetTy,
1797-
fn_def_id: DefId,
1795+
fn_def_id: LocalDefId,
17981796
span: Span,
17991797
) -> hir::GenericBound<'hir> {
18001798
// Compute the `T` in `Future<Output = T>` from the return type.
@@ -1805,7 +1803,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
18051803
// generates.
18061804
let context = ImplTraitContext::ReturnPositionOpaqueTy {
18071805
fn_def_id,
1808-
origin: hir::OpaqueTyOrigin::FnReturn,
1806+
origin: hir::OpaqueTyOrigin::FnReturn(fn_def_id),
18091807
};
18101808
self.lower_ty(ty, context)
18111809
}
@@ -2442,17 +2440,12 @@ impl<'hir> GenericArgsCtor<'hir> {
24422440
}
24432441
}
24442442

2443+
#[tracing::instrument(level = "debug")]
24452444
fn lifetimes_from_impl_trait_bounds(
24462445
opaque_ty_id: NodeId,
24472446
bounds: hir::GenericBounds<'_>,
24482447
lifetimes_to_include: Option<&FxHashSet<hir::LifetimeName>>,
24492448
) -> Vec<(hir::LifetimeName, Span)> {
2450-
debug!(
2451-
"lifetimes_from_impl_trait_bounds(opaque_ty_id={:?}, \
2452-
bounds={:#?})",
2453-
opaque_ty_id, bounds,
2454-
);
2455-
24562449
// This visitor walks over `impl Trait` bounds and creates defs for all lifetimes that
24572450
// appear in the bounds, excluding lifetimes that are created within the bounds.
24582451
// 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
@@ -2248,17 +2248,16 @@ pub struct BareFnTy<'hir> {
22482248
pub struct OpaqueTy<'hir> {
22492249
pub generics: Generics<'hir>,
22502250
pub bounds: GenericBounds<'hir>,
2251-
pub impl_trait_fn: Option<DefId>,
22522251
pub origin: OpaqueTyOrigin,
22532252
}
22542253

22552254
/// From whence the opaque type came.
22562255
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Decodable, Debug, HashStable_Generic)]
22572256
pub enum OpaqueTyOrigin {
22582257
/// `-> impl Trait`
2259-
FnReturn,
2258+
FnReturn(LocalDefId),
22602259
/// `async fn`
2261-
AsyncFn,
2260+
AsyncFn(LocalDefId),
22622261
/// type aliases: `type Foo = impl Trait;`
22632262
TyAlias,
22642263
}
@@ -2809,7 +2808,9 @@ impl ItemKind<'_> {
28092808
Some(match *self {
28102809
ItemKind::Fn(_, ref generics, _)
28112810
| ItemKind::TyAlias(_, ref generics)
2812-
| ItemKind::OpaqueTy(OpaqueTy { ref generics, impl_trait_fn: None, .. })
2811+
| ItemKind::OpaqueTy(OpaqueTy {
2812+
ref generics, origin: OpaqueTyOrigin::TyAlias, ..
2813+
})
28132814
| ItemKind::Enum(_, ref generics)
28142815
| ItemKind::Struct(_, ref generics)
28152816
| 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>
@@ -463,20 +463,24 @@ impl<'a, 'tcx> Instantiator<'a, 'tcx> {
463463
let parent_def_id = self.infcx.defining_use_anchor;
464464
let (in_definition_scope, origin) = match tcx.hir().expect_item(def_id).kind
465465
{
466+
// Async `impl Trait`
467+
hir::ItemKind::OpaqueTy(hir::OpaqueTy {
468+
origin: hir::OpaqueTyOrigin::AsyncFn(parent),
469+
..
470+
}) => (parent == parent_def_id, hir::OpaqueTyOrigin::AsyncFn(parent)),
466471
// Anonymous `impl Trait`
467472
hir::ItemKind::OpaqueTy(hir::OpaqueTy {
468-
impl_trait_fn: Some(parent),
469-
origin,
473+
origin: hir::OpaqueTyOrigin::FnReturn(parent),
470474
..
471-
}) => (parent == parent_def_id.to_def_id(), origin),
475+
}) => (parent == parent_def_id, hir::OpaqueTyOrigin::FnReturn(parent)),
472476
// Named `type Foo = impl Bar;`
473477
hir::ItemKind::OpaqueTy(hir::OpaqueTy {
474-
impl_trait_fn: None,
475-
origin,
478+
origin: hir::OpaqueTyOrigin::TyAlias,
476479
..
477-
}) => {
478-
(may_define_opaque_type(tcx, parent_def_id, opaque_hir_id), origin)
479-
}
480+
}) => (
481+
may_define_opaque_type(tcx, parent_def_id, opaque_hir_id),
482+
hir::OpaqueTyOrigin::TyAlias,
483+
),
480484
ref itemkind => {
481485
span_bug!(
482486
self.value_span,

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
@@ -2336,9 +2336,16 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
23362336
let def_id = item_id.def_id.to_def_id();
23372337

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

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)