Use named fields for OpaqueTyOrigin

This commit is contained in:
Michael Goulet 2024-10-02 22:04:18 -04:00
parent f95bdf453e
commit cb7e3695e8
19 changed files with 53 additions and 40 deletions

View File

@ -1555,7 +1555,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
.map(|(ident, id, _)| Lifetime { id, ident })
.collect()
}
hir::OpaqueTyOrigin::FnReturn(..) => {
hir::OpaqueTyOrigin::FnReturn { .. } => {
if matches!(
fn_kind.expect("expected RPITs to be lowered with a FnKind"),
FnDeclKind::Impl | FnDeclKind::Trait
@ -1576,7 +1576,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
lifetime_collector::lifetimes_in_bounds(self.resolver, bounds)
}
}
hir::OpaqueTyOrigin::AsyncFn(..) => {
hir::OpaqueTyOrigin::AsyncFn { .. } => {
unreachable!("should be using `lower_async_fn_ret_ty`")
}
}
@ -1867,7 +1867,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
| FnDeclKind::Inherent
| FnDeclKind::Trait
| FnDeclKind::Impl => ImplTraitContext::OpaqueTy {
origin: hir::OpaqueTyOrigin::FnReturn(self.local_def_id(fn_node_id)),
origin: hir::OpaqueTyOrigin::FnReturn {
parent: self.local_def_id(fn_node_id),
},
fn_kind: Some(kind),
},
FnDeclKind::ExternFn => {
@ -1952,7 +1954,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
let opaque_ty_ref = self.lower_opaque_inner(
opaque_ty_node_id,
hir::OpaqueTyOrigin::AsyncFn(fn_def_id),
hir::OpaqueTyOrigin::AsyncFn { parent: fn_def_id },
matches!(fn_kind, FnDeclKind::Trait),
captured_lifetimes,
span,
@ -1963,7 +1965,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
coro,
opaque_ty_span,
ImplTraitContext::OpaqueTy {
origin: hir::OpaqueTyOrigin::FnReturn(fn_def_id),
origin: hir::OpaqueTyOrigin::FnReturn { parent: fn_def_id },
fn_kind: Some(fn_kind),
},
);

View File

@ -503,8 +503,8 @@ impl<'tcx> LazyOpaqueTyEnv<'tcx> {
let &Self { tcx, def_id, .. } = self;
let origin = tcx.opaque_type_origin(def_id);
let parent = match origin {
hir::OpaqueTyOrigin::FnReturn(parent)
| hir::OpaqueTyOrigin::AsyncFn(parent)
hir::OpaqueTyOrigin::FnReturn { parent }
| hir::OpaqueTyOrigin::AsyncFn { parent }
| hir::OpaqueTyOrigin::TyAlias { parent, .. } => parent,
};
let param_env = tcx.param_env(parent);

View File

@ -2806,9 +2806,15 @@ pub struct PreciseCapturingNonLifetimeArg {
#[derive(Copy, Clone, PartialEq, Eq, Debug, HashStable_Generic)]
pub enum OpaqueTyOrigin {
/// `-> impl Trait`
FnReturn(LocalDefId),
FnReturn {
/// The defining function.
parent: LocalDefId,
},
/// `async fn`
AsyncFn(LocalDefId),
AsyncFn {
/// The defining function.
parent: LocalDefId,
},
/// type aliases: `type Foo = impl Trait;`
TyAlias {
/// The type alias or associated type parent of the TAIT/ATPIT

View File

@ -336,9 +336,9 @@ fn check_opaque_meets_bounds<'tcx>(
origin: &hir::OpaqueTyOrigin,
) -> Result<(), ErrorGuaranteed> {
let defining_use_anchor = match *origin {
hir::OpaqueTyOrigin::FnReturn(did)
| hir::OpaqueTyOrigin::AsyncFn(did)
| hir::OpaqueTyOrigin::TyAlias { parent: did, .. } => did,
hir::OpaqueTyOrigin::FnReturn { parent }
| hir::OpaqueTyOrigin::AsyncFn { parent }
| hir::OpaqueTyOrigin::TyAlias { parent, .. } => parent,
};
let param_env = tcx.param_env(defining_use_anchor);
@ -346,8 +346,8 @@ fn check_opaque_meets_bounds<'tcx>(
let ocx = ObligationCtxt::new_with_diagnostics(&infcx);
let args = match *origin {
hir::OpaqueTyOrigin::FnReturn(parent)
| hir::OpaqueTyOrigin::AsyncFn(parent)
hir::OpaqueTyOrigin::FnReturn { parent }
| hir::OpaqueTyOrigin::AsyncFn { parent }
| hir::OpaqueTyOrigin::TyAlias { parent, .. } => GenericArgs::identity_for_item(
tcx, parent,
)
@ -409,7 +409,7 @@ fn check_opaque_meets_bounds<'tcx>(
let outlives_env = OutlivesEnvironment::with_bounds(param_env, implied_bounds);
ocx.resolve_regions_and_report_errors(defining_use_anchor, &outlives_env)?;
if let hir::OpaqueTyOrigin::FnReturn(..) | hir::OpaqueTyOrigin::AsyncFn(..) = origin {
if let hir::OpaqueTyOrigin::FnReturn { .. } | hir::OpaqueTyOrigin::AsyncFn { .. } = origin {
// HACK: this should also fall through to the hidden type check below, but the original
// implementation had a bug where equivalent lifetimes are not identical. This caused us
// 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) {
check_opaque_precise_captures(tcx, def_id);
let origin = tcx.opaque_type_origin(def_id);
if let hir::OpaqueTyOrigin::FnReturn(fn_def_id)
| hir::OpaqueTyOrigin::AsyncFn(fn_def_id) = origin
if let hir::OpaqueTyOrigin::FnReturn { parent: fn_def_id }
| hir::OpaqueTyOrigin::AsyncFn { parent: fn_def_id } = origin
&& let hir::Node::TraitItem(trait_item) = tcx.hir_node_by_def_id(fn_def_id)
&& let (_, hir::TraitFn::Required(..)) = trait_item.expect_fn()
{

View File

@ -94,8 +94,8 @@ pub(super) fn check_refining_return_position_impl_trait_in_trait<'tcx>(
if !tcx.hir().get_if_local(impl_opaque.def_id).is_some_and(|node| {
matches!(
node.expect_item().expect_opaque_ty().origin,
hir::OpaqueTyOrigin::AsyncFn(def_id) | hir::OpaqueTyOrigin::FnReturn(def_id)
if def_id == impl_m.def_id.expect_local()
hir::OpaqueTyOrigin::AsyncFn { parent } | hir::OpaqueTyOrigin::FnReturn { parent }
if parent == impl_m.def_id.expect_local()
)
}) {
report_mismatched_rpitit_signature(

View File

@ -210,7 +210,8 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
Node::Item(item) => match item.kind {
ItemKind::OpaqueTy(&hir::OpaqueTy {
origin:
hir::OpaqueTyOrigin::FnReturn(fn_def_id) | hir::OpaqueTyOrigin::AsyncFn(fn_def_id),
hir::OpaqueTyOrigin::FnReturn { parent: fn_def_id }
| hir::OpaqueTyOrigin::AsyncFn { parent: fn_def_id },
in_trait,
..
}) => {

View File

@ -388,8 +388,8 @@ pub(super) fn explicit_item_bounds_with_filter(
span,
..
}) => {
let (hir::OpaqueTyOrigin::FnReturn(fn_def_id)
| hir::OpaqueTyOrigin::AsyncFn(fn_def_id)) = *origin
let (hir::OpaqueTyOrigin::FnReturn { parent: fn_def_id }
| hir::OpaqueTyOrigin::AsyncFn { parent: fn_def_id }) = *origin
else {
span_bug!(*span, "RPITIT cannot be a TAIT, but got origin {origin:?}");
};

View File

@ -515,8 +515,8 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
}
hir::ItemKind::OpaqueTy(&hir::OpaqueTy {
origin:
hir::OpaqueTyOrigin::FnReturn(parent)
| hir::OpaqueTyOrigin::AsyncFn(parent)
hir::OpaqueTyOrigin::FnReturn { parent }
| hir::OpaqueTyOrigin::AsyncFn { parent }
| hir::OpaqueTyOrigin::TyAlias { parent, .. },
generics,
..

View File

@ -618,7 +618,8 @@ pub(super) fn type_of_opaque(
// Opaque types desugared from `impl Trait`.
ItemKind::OpaqueTy(&OpaqueTy {
origin:
hir::OpaqueTyOrigin::FnReturn(owner) | hir::OpaqueTyOrigin::AsyncFn(owner),
hir::OpaqueTyOrigin::FnReturn { parent: owner }
| hir::OpaqueTyOrigin::AsyncFn { parent: owner },
in_trait,
..
}) => {

View File

@ -602,7 +602,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.map(|(k, _)| (k.def_id, k.args))?,
_ => return None,
};
let hir::OpaqueTyOrigin::FnReturn(parent_def_id) = self.tcx.opaque_type_origin(def_id)
let hir::OpaqueTyOrigin::FnReturn { parent: parent_def_id } =
self.tcx.opaque_type_origin(def_id)
else {
return None;
};

View File

@ -259,8 +259,8 @@ where
// If it's owned by this function
&& let opaque =
self.tcx.hir_node_by_def_id(opaque_def_id).expect_item().expect_opaque_ty()
&& let hir::OpaqueTyOrigin::FnReturn(parent_def_id) = opaque.origin
&& parent_def_id == self.parent_def_id
&& let hir::OpaqueTyOrigin::FnReturn { parent } = opaque.origin
&& parent == self.parent_def_id
{
let opaque_span = self.tcx.def_span(opaque_def_id);
let new_capture_rules =

View File

@ -77,7 +77,7 @@ impl<'tcx> LateLintPass<'tcx> for OpaqueHiddenInferredBound {
// That's because although we may have an opaque type on the function,
// it won't have a hidden type, so proving predicates about it is
// not really meaningful.
if let hir::OpaqueTyOrigin::FnReturn(method_def_id) = opaque.origin
if let hir::OpaqueTyOrigin::FnReturn { parent: method_def_id } = opaque.origin
&& let hir::Node::TraitItem(trait_item) = cx.tcx.hir_node_by_def_id(method_def_id)
&& !trait_item.defaultness.has_value()
{
@ -103,7 +103,7 @@ impl<'tcx> LateLintPass<'tcx> for OpaqueHiddenInferredBound {
&& cx.tcx.parent(opaque_ty.def_id) == def_id
&& matches!(
opaque.origin,
hir::OpaqueTyOrigin::FnReturn(_) | hir::OpaqueTyOrigin::AsyncFn(_)
hir::OpaqueTyOrigin::FnReturn { .. } | hir::OpaqueTyOrigin::AsyncFn { .. }
)
{
return;
@ -114,7 +114,7 @@ impl<'tcx> LateLintPass<'tcx> for OpaqueHiddenInferredBound {
// return type is well-formed in traits even when `Self` isn't sized.
if let ty::Param(param_ty) = *proj_term.kind()
&& param_ty.name == kw::SelfUpper
&& matches!(opaque.origin, hir::OpaqueTyOrigin::AsyncFn(_))
&& matches!(opaque.origin, hir::OpaqueTyOrigin::AsyncFn { .. })
&& opaque.in_trait
{
return;

View File

@ -1186,9 +1186,9 @@ fn should_encode_type(tcx: TyCtxt<'_>, def_id: LocalDefId, def_kind: DefKind) ->
DefKind::OpaqueTy => {
let origin = tcx.opaque_type_origin(def_id);
if let hir::OpaqueTyOrigin::FnReturn(fn_def_id)
| hir::OpaqueTyOrigin::AsyncFn(fn_def_id) = origin
&& let hir::Node::TraitItem(trait_item) = tcx.hir_node_by_def_id(fn_def_id)
if let hir::OpaqueTyOrigin::FnReturn { parent }
| hir::OpaqueTyOrigin::AsyncFn { parent } = origin
&& let hir::Node::TraitItem(trait_item) = tcx.hir_node_by_def_id(parent)
&& let (_, hir::TraitFn::Required(..)) = trait_item.expect_fn()
{
false

View File

@ -1271,7 +1271,7 @@ fn suggest_precise_capturing<'tcx>(
let hir::OpaqueTy { bounds, origin, .. } =
tcx.hir_node_by_def_id(opaque_def_id).expect_item().expect_opaque_ty();
let hir::OpaqueTyOrigin::FnReturn(fn_def_id) = *origin else {
let hir::OpaqueTyOrigin::FnReturn { parent: fn_def_id } = *origin else {
return;
};

View File

@ -2580,7 +2580,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
def_id: DefId,
) -> ErrorGuaranteed {
let name = match self.tcx.opaque_type_origin(def_id.expect_local()) {
hir::OpaqueTyOrigin::FnReturn(_) | hir::OpaqueTyOrigin::AsyncFn(_) => {
hir::OpaqueTyOrigin::FnReturn { .. } | hir::OpaqueTyOrigin::AsyncFn { .. } => {
"opaque type".to_string()
}
hir::OpaqueTyOrigin::TyAlias { .. } => {

View File

@ -379,7 +379,8 @@ fn associated_type_for_impl_trait_in_trait(
tcx: TyCtxt<'_>,
opaque_ty_def_id: LocalDefId,
) -> LocalDefId {
let (hir::OpaqueTyOrigin::FnReturn(fn_def_id) | hir::OpaqueTyOrigin::AsyncFn(fn_def_id)) =
let (hir::OpaqueTyOrigin::FnReturn { parent: fn_def_id }
| hir::OpaqueTyOrigin::AsyncFn { parent: fn_def_id }) =
tcx.opaque_type_origin(opaque_ty_def_id)
else {
bug!("expected opaque for {opaque_ty_def_id:?}");

View File

@ -141,7 +141,8 @@ impl<'tcx> OpaqueTypeCollector<'tcx> {
let origin = self.tcx.opaque_type_origin(alias_ty.def_id.expect_local());
trace!(?origin);
match origin {
rustc_hir::OpaqueTyOrigin::FnReturn(_) | rustc_hir::OpaqueTyOrigin::AsyncFn(_) => {}
rustc_hir::OpaqueTyOrigin::FnReturn { .. }
| rustc_hir::OpaqueTyOrigin::AsyncFn { .. } => {}
rustc_hir::OpaqueTyOrigin::TyAlias { in_assoc_ty, .. } => {
if !in_assoc_ty && !self.check_tait_defining_scope(alias_ty.def_id.expect_local()) {
return;

View File

@ -515,7 +515,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
self.add_to_current_mod(item, renamed, import_id);
}
hir::ItemKind::OpaqueTy(hir::OpaqueTy {
origin: hir::OpaqueTyOrigin::AsyncFn(_) | hir::OpaqueTyOrigin::FnReturn(_),
origin: hir::OpaqueTyOrigin::AsyncFn { .. } | hir::OpaqueTyOrigin::FnReturn { .. },
..
}) => {
// return-position impl traits are never nameable, and should never be documented.

View File

@ -313,7 +313,7 @@ fn extract_future_output<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<&
kind: ItemKind::OpaqueTy(opaque),
..
} = item
&& let OpaqueTyOrigin::AsyncFn(_) = opaque.origin
&& let OpaqueTyOrigin::AsyncFn { .. } = opaque.origin
&& let [GenericBound::Trait(trait_ref, _)] = &opaque.bounds
&& let Some(segment) = trait_ref.trait_ref.path.segments.last()
&& let Some(generic_args) = segment.args