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

View File

@ -503,8 +503,8 @@ fn get_canonical_args(&self) -> ty::GenericArgsRef<'tcx> {
let &Self { tcx, def_id, .. } = self; let &Self { tcx, def_id, .. } = self;
let origin = tcx.opaque_type_origin(def_id); let origin = tcx.opaque_type_origin(def_id);
let parent = match origin { let parent = match origin {
hir::OpaqueTyOrigin::FnReturn(parent) hir::OpaqueTyOrigin::FnReturn { parent }
| hir::OpaqueTyOrigin::AsyncFn(parent) | hir::OpaqueTyOrigin::AsyncFn { parent }
| hir::OpaqueTyOrigin::TyAlias { parent, .. } => parent, | hir::OpaqueTyOrigin::TyAlias { parent, .. } => parent,
}; };
let param_env = tcx.param_env(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)] #[derive(Copy, Clone, PartialEq, Eq, Debug, HashStable_Generic)]
pub enum OpaqueTyOrigin { pub enum OpaqueTyOrigin {
/// `-> impl Trait` /// `-> impl Trait`
FnReturn(LocalDefId), FnReturn {
/// The defining function.
parent: LocalDefId,
},
/// `async fn` /// `async fn`
AsyncFn(LocalDefId), AsyncFn {
/// The defining function.
parent: LocalDefId,
},
/// type aliases: `type Foo = impl Trait;` /// type aliases: `type Foo = impl Trait;`
TyAlias { TyAlias {
/// The type alias or associated type parent of the TAIT/ATPIT /// 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, origin: &hir::OpaqueTyOrigin,
) -> Result<(), ErrorGuaranteed> { ) -> Result<(), ErrorGuaranteed> {
let defining_use_anchor = match *origin { let defining_use_anchor = match *origin {
hir::OpaqueTyOrigin::FnReturn(did) hir::OpaqueTyOrigin::FnReturn { parent }
| hir::OpaqueTyOrigin::AsyncFn(did) | hir::OpaqueTyOrigin::AsyncFn { parent }
| hir::OpaqueTyOrigin::TyAlias { parent: did, .. } => did, | hir::OpaqueTyOrigin::TyAlias { parent, .. } => parent,
}; };
let param_env = tcx.param_env(defining_use_anchor); 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 ocx = ObligationCtxt::new_with_diagnostics(&infcx);
let args = match *origin { let args = match *origin {
hir::OpaqueTyOrigin::FnReturn(parent) hir::OpaqueTyOrigin::FnReturn { parent }
| hir::OpaqueTyOrigin::AsyncFn(parent) | hir::OpaqueTyOrigin::AsyncFn { parent }
| hir::OpaqueTyOrigin::TyAlias { parent, .. } => GenericArgs::identity_for_item( | hir::OpaqueTyOrigin::TyAlias { parent, .. } => GenericArgs::identity_for_item(
tcx, parent, tcx, parent,
) )
@ -409,7 +409,7 @@ fn check_opaque_meets_bounds<'tcx>(
let outlives_env = OutlivesEnvironment::with_bounds(param_env, implied_bounds); let outlives_env = OutlivesEnvironment::with_bounds(param_env, implied_bounds);
ocx.resolve_regions_and_report_errors(defining_use_anchor, &outlives_env)?; 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 // 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 // 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 // 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); check_opaque_precise_captures(tcx, def_id);
let origin = tcx.opaque_type_origin(def_id); let origin = tcx.opaque_type_origin(def_id);
if let hir::OpaqueTyOrigin::FnReturn(fn_def_id) if let hir::OpaqueTyOrigin::FnReturn { parent: fn_def_id }
| hir::OpaqueTyOrigin::AsyncFn(fn_def_id) = origin | 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::Node::TraitItem(trait_item) = tcx.hir_node_by_def_id(fn_def_id)
&& let (_, hir::TraitFn::Required(..)) = trait_item.expect_fn() && 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| { if !tcx.hir().get_if_local(impl_opaque.def_id).is_some_and(|node| {
matches!( matches!(
node.expect_item().expect_opaque_ty().origin, node.expect_item().expect_opaque_ty().origin,
hir::OpaqueTyOrigin::AsyncFn(def_id) | hir::OpaqueTyOrigin::FnReturn(def_id) hir::OpaqueTyOrigin::AsyncFn { parent } | hir::OpaqueTyOrigin::FnReturn { parent }
if def_id == impl_m.def_id.expect_local() if parent == impl_m.def_id.expect_local()
) )
}) { }) {
report_mismatched_rpitit_signature( 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 { Node::Item(item) => match item.kind {
ItemKind::OpaqueTy(&hir::OpaqueTy { ItemKind::OpaqueTy(&hir::OpaqueTy {
origin: 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, in_trait,
.. ..
}) => { }) => {

View File

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

View File

@ -515,8 +515,8 @@ fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
} }
hir::ItemKind::OpaqueTy(&hir::OpaqueTy { hir::ItemKind::OpaqueTy(&hir::OpaqueTy {
origin: origin:
hir::OpaqueTyOrigin::FnReturn(parent) hir::OpaqueTyOrigin::FnReturn { parent }
| hir::OpaqueTyOrigin::AsyncFn(parent) | hir::OpaqueTyOrigin::AsyncFn { parent }
| hir::OpaqueTyOrigin::TyAlias { parent, .. }, | hir::OpaqueTyOrigin::TyAlias { parent, .. },
generics, generics,
.. ..

View File

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

View File

@ -602,7 +602,8 @@ pub(crate) fn return_position_impl_trait_from_match_expectation(
.map(|(k, _)| (k.def_id, k.args))?, .map(|(k, _)| (k.def_id, k.args))?,
_ => return None, _ => 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 { else {
return None; return None;
}; };

View File

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

View File

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

View File

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

View File

@ -1271,7 +1271,7 @@ fn suggest_precise_capturing<'tcx>(
let hir::OpaqueTy { bounds, origin, .. } = let hir::OpaqueTy { bounds, origin, .. } =
tcx.hir_node_by_def_id(opaque_def_id).expect_item().expect_opaque_ty(); 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; return;
}; };

View File

@ -2580,7 +2580,7 @@ fn report_opaque_type_auto_trait_leakage(
def_id: DefId, def_id: DefId,
) -> ErrorGuaranteed { ) -> ErrorGuaranteed {
let name = match self.tcx.opaque_type_origin(def_id.expect_local()) { 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() "opaque type".to_string()
} }
hir::OpaqueTyOrigin::TyAlias { .. } => { hir::OpaqueTyOrigin::TyAlias { .. } => {

View File

@ -379,7 +379,8 @@ fn associated_type_for_impl_trait_in_trait(
tcx: TyCtxt<'_>, tcx: TyCtxt<'_>,
opaque_ty_def_id: LocalDefId, opaque_ty_def_id: LocalDefId,
) -> 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) tcx.opaque_type_origin(opaque_ty_def_id)
else { else {
bug!("expected opaque for {opaque_ty_def_id:?}"); bug!("expected opaque for {opaque_ty_def_id:?}");

View File

@ -141,7 +141,8 @@ fn visit_opaque_ty(&mut self, alias_ty: ty::AliasTy<'tcx>) {
let origin = self.tcx.opaque_type_origin(alias_ty.def_id.expect_local()); let origin = self.tcx.opaque_type_origin(alias_ty.def_id.expect_local());
trace!(?origin); trace!(?origin);
match 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, .. } => { rustc_hir::OpaqueTyOrigin::TyAlias { in_assoc_ty, .. } => {
if !in_assoc_ty && !self.check_tait_defining_scope(alias_ty.def_id.expect_local()) { if !in_assoc_ty && !self.check_tait_defining_scope(alias_ty.def_id.expect_local()) {
return; return;

View File

@ -515,7 +515,7 @@ fn visit_item_inner(
self.add_to_current_mod(item, renamed, import_id); self.add_to_current_mod(item, renamed, import_id);
} }
hir::ItemKind::OpaqueTy(hir::OpaqueTy { 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. // 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), kind: ItemKind::OpaqueTy(opaque),
.. ..
} = item } = item
&& let OpaqueTyOrigin::AsyncFn(_) = opaque.origin && let OpaqueTyOrigin::AsyncFn { .. } = opaque.origin
&& let [GenericBound::Trait(trait_ref, _)] = &opaque.bounds && let [GenericBound::Trait(trait_ref, _)] = &opaque.bounds
&& let Some(segment) = trait_ref.trait_ref.path.segments.last() && let Some(segment) = trait_ref.trait_ref.path.segments.last()
&& let Some(generic_args) = segment.args && let Some(generic_args) = segment.args