Skip to content

Commit

Permalink
Use named fields for OpaqueTyOrigin
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Oct 3, 2024
1 parent f95bdf4 commit cb7e369
Show file tree
Hide file tree
Showing 19 changed files with 53 additions and 40 deletions.
12 changes: 7 additions & 5 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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`")
}
}
Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -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,
Expand All @@ -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),
},
);
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_borrowck/src/region_infer/opaque_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 8 additions & 2 deletions compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions compiler/rustc_hir_analysis/src/check/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,18 +336,18 @@ 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);

let infcx = tcx.infer_ctxt().with_opaque_type_inference(defining_use_anchor).build();
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,
)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_hir_analysis/src/collect/generics_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
..
}) => {
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_hir_analysis/src/collect/item_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:?}");
};
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
..
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_hir_analysis/src/collect/type_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
..
}) => {
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_hir_typeck/src/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_lint/src/impl_trait_overcaptures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_lint/src/opaque_hidden_inferred_bound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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;
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 { .. } => {
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_ty_utils/src/assoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:?}");
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_ty_utils/src/opaque_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/visit_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/tools/clippy/clippy_lints/src/len_zero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cb7e369

Please sign in to comment.