Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 940b45f

Browse files
authoredJan 31, 2025
Rollup merge of #136281 - nnethercote:rustc_hir_analysis, r=lcnr
`rustc_hir_analysis` cleanups Just some improvements I found while looking through this code. r? `@lcnr`
2 parents c19c4b9 + 483307f commit 940b45f

File tree

22 files changed

+224
-315
lines changed

22 files changed

+224
-315
lines changed
 

‎compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -424,12 +424,12 @@ fn compare_method_predicate_entailment<'tcx>(
424424
Ok(())
425425
}
426426

427-
struct RemapLateParam<'a, 'tcx> {
427+
struct RemapLateParam<'tcx> {
428428
tcx: TyCtxt<'tcx>,
429-
mapping: &'a FxIndexMap<ty::LateParamRegionKind, ty::LateParamRegionKind>,
429+
mapping: FxIndexMap<ty::LateParamRegionKind, ty::LateParamRegionKind>,
430430
}
431431

432-
impl<'tcx> TypeFolder<TyCtxt<'tcx>> for RemapLateParam<'_, 'tcx> {
432+
impl<'tcx> TypeFolder<TyCtxt<'tcx>> for RemapLateParam<'tcx> {
433433
fn cx(&self) -> TyCtxt<'tcx> {
434434
self.tcx
435435
}

‎compiler/rustc_hir_analysis/src/check/compare_impl_item/refine.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,7 @@ fn report_mismatched_rpitit_signature<'tcx>(
299299
})
300300
.collect();
301301

302-
let mut return_ty =
303-
trait_m_sig.output().fold_with(&mut super::RemapLateParam { tcx, mapping: &mapping });
302+
let mut return_ty = trait_m_sig.output().fold_with(&mut super::RemapLateParam { tcx, mapping });
304303

305304
if tcx.asyncness(impl_m_def_id).is_async() && tcx.asyncness(trait_m_def_id).is_async() {
306305
let ty::Alias(ty::Projection, future_ty) = return_ty.kind() else {

‎compiler/rustc_hir_analysis/src/check/dropck.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
// FIXME(@lcnr): Move this module out of `rustc_hir_analysis`.
2-
//
3-
// We don't do any drop checking during hir typeck.
4-
51
use rustc_data_structures::fx::FxHashSet;
62
use rustc_errors::codes::*;
73
use rustc_errors::{ErrorGuaranteed, struct_span_code_err};
@@ -32,7 +28,10 @@ use crate::hir::def_id::{DefId, LocalDefId};
3228
/// struct/enum definition for the nominal type itself (i.e.
3329
/// cannot do `struct S<T>; impl<T:Clone> Drop for S<T> { ... }`).
3430
///
35-
pub fn check_drop_impl(tcx: TyCtxt<'_>, drop_impl_did: DefId) -> Result<(), ErrorGuaranteed> {
31+
pub(crate) fn check_drop_impl(
32+
tcx: TyCtxt<'_>,
33+
drop_impl_did: DefId,
34+
) -> Result<(), ErrorGuaranteed> {
3635
match tcx.impl_polarity(drop_impl_did) {
3736
ty::ImplPolarity::Positive => {}
3837
ty::ImplPolarity::Negative => {

‎compiler/rustc_hir_analysis/src/check/intrinsic.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,8 @@ pub fn check_intrinsic_type(
199199
let split: Vec<&str> = name_str.split('_').collect();
200200
assert!(split.len() >= 2, "Atomic intrinsic in an incorrect format");
201201

202-
//We only care about the operation here
202+
// Each atomic op has variants with different suffixes (`_seq_cst`, `_acquire`, etc.). Use
203+
// string ops to strip the suffixes, because the variants all get the same treatment here.
203204
let (n_tps, inputs, output) = match split[1] {
204205
"cxchg" | "cxchgweak" => (
205206
1,

‎compiler/rustc_hir_analysis/src/check/mod.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -455,18 +455,14 @@ fn fn_sig_suggestion<'tcx>(
455455
let mut output = sig.output();
456456

457457
let asyncness = if tcx.asyncness(assoc.def_id).is_async() {
458-
output = if let ty::Alias(_, alias_ty) = *output.kind() {
459-
tcx.explicit_item_self_bounds(alias_ty.def_id)
458+
output = if let ty::Alias(_, alias_ty) = *output.kind()
459+
&& let Some(output) = tcx
460+
.explicit_item_self_bounds(alias_ty.def_id)
460461
.iter_instantiated_copied(tcx, alias_ty.args)
461462
.find_map(|(bound, _)| {
462463
bound.as_projection_clause()?.no_bound_vars()?.term.as_type()
463-
})
464-
.unwrap_or_else(|| {
465-
span_bug!(
466-
ident.span,
467-
"expected async fn to have `impl Future` output, but it returns {output}"
468-
)
469-
})
464+
}) {
465+
output
470466
} else {
471467
span_bug!(
472468
ident.span,

‎compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2267,14 +2267,12 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> {
22672267

22682268
fn check_mod_type_wf(tcx: TyCtxt<'_>, module: LocalModDefId) -> Result<(), ErrorGuaranteed> {
22692269
let items = tcx.hir_module_items(module);
2270-
let mut res = items.par_items(|item| tcx.ensure().check_well_formed(item.owner_id.def_id));
2271-
res =
2272-
res.and(items.par_impl_items(|item| tcx.ensure().check_well_formed(item.owner_id.def_id)));
2273-
res =
2274-
res.and(items.par_trait_items(|item| tcx.ensure().check_well_formed(item.owner_id.def_id)));
2275-
res = res
2276-
.and(items.par_foreign_items(|item| tcx.ensure().check_well_formed(item.owner_id.def_id)));
2277-
res = res.and(items.par_opaques(|item| tcx.ensure().check_well_formed(item)));
2270+
let res = items
2271+
.par_items(|item| tcx.ensure().check_well_formed(item.owner_id.def_id))
2272+
.and(items.par_impl_items(|item| tcx.ensure().check_well_formed(item.owner_id.def_id)))
2273+
.and(items.par_trait_items(|item| tcx.ensure().check_well_formed(item.owner_id.def_id)))
2274+
.and(items.par_foreign_items(|item| tcx.ensure().check_well_formed(item.owner_id.def_id)))
2275+
.and(items.par_opaques(|item| tcx.ensure().check_well_formed(item)));
22782276
if module == LocalModDefId::CRATE_DEF_ID {
22792277
super::entry::check_for_entry_fn(tcx);
22802278
}

‎compiler/rustc_hir_analysis/src/coherence/builtin.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -404,17 +404,12 @@ pub(crate) fn coerce_unsized_info<'tcx>(
404404
check_mutbl(mt_a, mt_b, &|ty| Ty::new_imm_ref(tcx, r_b, ty))
405405
}
406406

407-
(&ty::Ref(_, ty_a, mutbl_a), &ty::RawPtr(ty_b, mutbl_b)) => check_mutbl(
408-
ty::TypeAndMut { ty: ty_a, mutbl: mutbl_a },
409-
ty::TypeAndMut { ty: ty_b, mutbl: mutbl_b },
410-
&|ty| Ty::new_imm_ptr(tcx, ty),
411-
),
412-
413-
(&ty::RawPtr(ty_a, mutbl_a), &ty::RawPtr(ty_b, mutbl_b)) => check_mutbl(
414-
ty::TypeAndMut { ty: ty_a, mutbl: mutbl_a },
415-
ty::TypeAndMut { ty: ty_b, mutbl: mutbl_b },
416-
&|ty| Ty::new_imm_ptr(tcx, ty),
417-
),
407+
(&ty::Ref(_, ty_a, mutbl_a), &ty::RawPtr(ty_b, mutbl_b))
408+
| (&ty::RawPtr(ty_a, mutbl_a), &ty::RawPtr(ty_b, mutbl_b)) => {
409+
let mt_a = ty::TypeAndMut { ty: ty_a, mutbl: mutbl_a };
410+
let mt_b = ty::TypeAndMut { ty: ty_b, mutbl: mutbl_b };
411+
check_mutbl(mt_a, mt_b, &|ty| Ty::new_imm_ptr(tcx, ty))
412+
}
418413

419414
(&ty::Adt(def_a, args_a), &ty::Adt(def_b, args_b))
420415
if def_a.is_struct() && def_b.is_struct() =>

‎compiler/rustc_hir_analysis/src/coherence/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,12 @@ fn coherent_trait(tcx: TyCtxt<'_>, def_id: DefId) -> Result<(), ErrorGuaranteed>
158158
let trait_ref = trait_header.trait_ref.instantiate_identity();
159159
let trait_def = tcx.trait_def(trait_ref.def_id);
160160

161-
res = res.and(check_impl(tcx, impl_def_id, trait_ref, trait_def));
162-
res = res.and(check_object_overlap(tcx, impl_def_id, trait_ref));
163-
164-
res = res.and(unsafety::check_item(tcx, impl_def_id, trait_header, trait_def));
165-
res = res.and(tcx.ensure().orphan_check_impl(impl_def_id));
166-
res = res.and(builtin::check_trait(tcx, def_id, impl_def_id, trait_header));
161+
res = res
162+
.and(check_impl(tcx, impl_def_id, trait_ref, trait_def))
163+
.and(check_object_overlap(tcx, impl_def_id, trait_ref))
164+
.and(unsafety::check_item(tcx, impl_def_id, trait_header, trait_def))
165+
.and(tcx.ensure().orphan_check_impl(impl_def_id))
166+
.and(builtin::check_trait(tcx, def_id, impl_def_id, trait_header));
167167
}
168168

169169
res

‎compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ mod type_of;
5757

5858
///////////////////////////////////////////////////////////////////////////
5959

60-
pub fn provide(providers: &mut Providers) {
60+
pub(crate) fn provide(providers: &mut Providers) {
6161
resolve_bound_vars::provide(providers);
6262
*providers = Providers {
6363
type_of: type_of::type_of,
@@ -122,7 +122,7 @@ pub fn provide(providers: &mut Providers) {
122122
/// `ItemCtxt` is parameterized by a `DefId` that it uses to satisfy
123123
/// `probe_ty_param_bounds` requests, drawing the information from
124124
/// the HIR (`hir::Generics`), recursively.
125-
pub struct ItemCtxt<'tcx> {
125+
pub(crate) struct ItemCtxt<'tcx> {
126126
tcx: TyCtxt<'tcx>,
127127
item_def_id: LocalDefId,
128128
tainted_by_errors: Cell<Option<ErrorGuaranteed>>,
@@ -148,7 +148,7 @@ impl<'v> Visitor<'v> for HirPlaceholderCollector {
148148
}
149149
}
150150

151-
pub struct CollectItemTypesVisitor<'tcx> {
151+
pub(crate) struct CollectItemTypesVisitor<'tcx> {
152152
pub tcx: TyCtxt<'tcx>,
153153
}
154154

@@ -364,19 +364,19 @@ fn bad_placeholder<'cx, 'tcx>(
364364
}
365365

366366
impl<'tcx> ItemCtxt<'tcx> {
367-
pub fn new(tcx: TyCtxt<'tcx>, item_def_id: LocalDefId) -> ItemCtxt<'tcx> {
367+
pub(crate) fn new(tcx: TyCtxt<'tcx>, item_def_id: LocalDefId) -> ItemCtxt<'tcx> {
368368
ItemCtxt { tcx, item_def_id, tainted_by_errors: Cell::new(None) }
369369
}
370370

371-
pub fn lower_ty(&self, hir_ty: &hir::Ty<'tcx>) -> Ty<'tcx> {
371+
pub(crate) fn lower_ty(&self, hir_ty: &hir::Ty<'tcx>) -> Ty<'tcx> {
372372
self.lowerer().lower_ty(hir_ty)
373373
}
374374

375-
pub fn hir_id(&self) -> hir::HirId {
375+
pub(crate) fn hir_id(&self) -> hir::HirId {
376376
self.tcx.local_def_id_to_hir_id(self.item_def_id)
377377
}
378378

379-
pub fn node(&self) -> hir::Node<'tcx> {
379+
pub(crate) fn node(&self) -> hir::Node<'tcx> {
380380
self.tcx.hir_node(self.hir_id())
381381
}
382382

‎compiler/rustc_hir_analysis/src/delegation.rs

Lines changed: 147 additions & 213 deletions
Large diffs are not rendered by default.

‎compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ pub fn lower_generic_args<'tcx: 'a, 'a>(
273273

274274
// We lower to an infer even when the feature gate is not enabled
275275
// as it is useful for diagnostics to be able to see a `ConstKind::Infer`
276-
args.push(ctx.provided_kind(&args, param, arg));
276+
args.push(ctx.provided_kind(param, arg));
277277
args_iter.next();
278278
params.next();
279279
}

‎compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,6 @@ pub trait GenericArgsLowerer<'a, 'tcx> {
296296

297297
fn provided_kind(
298298
&mut self,
299-
preceding_args: &[ty::GenericArg<'tcx>],
300299
param: &ty::GenericParamDef,
301300
arg: &GenericArg<'tcx>,
302301
) -> ty::GenericArg<'tcx>;
@@ -480,7 +479,6 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
480479

481480
fn provided_kind(
482481
&mut self,
483-
_preceding_args: &[ty::GenericArg<'tcx>],
484482
param: &ty::GenericParamDef,
485483
arg: &GenericArg<'tcx>,
486484
) -> ty::GenericArg<'tcx> {

‎compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
//! impl<T, I: Iterator<Item=T>> SpecExtend<T> for I { /* default impl */ }
3535
//! ```
3636
//!
37-
//! We get that the generic pamameters for `impl2` are `[T, std::vec::IntoIter<T>]`.
37+
//! We get that the generic parameters for `impl2` are `[T, std::vec::IntoIter<T>]`.
3838
//! `T` is constrained to be `<I as Iterator>::Item`, so we check only
3939
//! `std::vec::IntoIter<T>` for repeated parameters, which it doesn't have. The
4040
//! predicates of `impl1` are only `T: Sized`, which is also a predicate of
@@ -119,7 +119,6 @@ fn check_always_applicable(
119119
impl2_node: Node,
120120
) -> Result<(), ErrorGuaranteed> {
121121
let span = tcx.def_span(impl1_def_id);
122-
let mut res = check_has_items(tcx, impl1_def_id, impl2_node, span);
123122

124123
let (impl1_args, impl2_args) = get_impl_args(tcx, impl1_def_id, impl2_node)?;
125124
let impl2_def_id = impl2_node.def_id();
@@ -131,11 +130,10 @@ fn check_always_applicable(
131130
unconstrained_parent_impl_args(tcx, impl2_def_id, impl2_args)
132131
};
133132

134-
res = res.and(check_static_lifetimes(tcx, &parent_args, span));
135-
res = res.and(check_duplicate_params(tcx, impl1_args, parent_args, span));
136-
res = res.and(check_predicates(tcx, impl1_def_id, impl1_args, impl2_node, impl2_args, span));
137-
138-
res
133+
check_has_items(tcx, impl1_def_id, impl2_node, span)
134+
.and(check_static_lifetimes(tcx, &parent_args, span))
135+
.and(check_duplicate_params(tcx, impl1_args, parent_args, span))
136+
.and(check_predicates(tcx, impl1_def_id, impl1_args, impl2_node, impl2_args, span))
139137
}
140138

141139
fn check_has_items(

‎compiler/rustc_hir_analysis/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,11 @@ pub mod autoderef;
8383
mod bounds;
8484
mod check_unused;
8585
mod coherence;
86-
mod delegation;
87-
pub mod hir_ty_lowering;
88-
// FIXME: This module shouldn't be public.
89-
pub mod collect;
86+
mod collect;
9087
mod constrained_generic_params;
88+
mod delegation;
9189
mod errors;
90+
pub mod hir_ty_lowering;
9291
pub mod hir_wf_check;
9392
mod impl_wf_check;
9493
mod outlives;
@@ -104,7 +103,8 @@ use rustc_middle::ty::{self, Const, Ty, TyCtxt};
104103
use rustc_span::Span;
105104
use rustc_trait_selection::traits;
106105

107-
use self::hir_ty_lowering::{FeedConstTy, HirTyLowerer};
106+
pub use crate::collect::suggest_impl_trait;
107+
use crate::hir_ty_lowering::{FeedConstTy, HirTyLowerer};
108108

109109
rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
110110

‎compiler/rustc_hir_analysis/src/outlives/implicit_infer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub(super) fn infer_predicates(
2424

2525
// If new predicates were added then we need to re-calculate
2626
// all crates since there could be new implied predicates.
27-
'outer: loop {
27+
loop {
2828
let mut predicates_added = false;
2929

3030
// Visit all the crates and infer predicates
@@ -90,7 +90,7 @@ pub(super) fn infer_predicates(
9090
}
9191

9292
if !predicates_added {
93-
break 'outer;
93+
break;
9494
}
9595
}
9696

‎compiler/rustc_hir_analysis/src/variance/mod.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ mod solve;
2727

2828
pub(crate) mod dump;
2929

30-
/// Code for transforming variances.
31-
mod xform;
32-
3330
pub(crate) fn provide(providers: &mut Providers) {
3431
*providers = Providers { variances_of, crate_variances, ..*providers };
3532
}

‎compiler/rustc_hir_analysis/src/variance/solve.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,26 @@ use tracing::debug;
1212
use super::constraints::*;
1313
use super::terms::VarianceTerm::*;
1414
use super::terms::*;
15-
use super::xform::*;
1615

16+
fn glb(v1: ty::Variance, v2: ty::Variance) -> ty::Variance {
17+
// Greatest lower bound of the variance lattice as defined in The Paper:
18+
//
19+
// *
20+
// - +
21+
// o
22+
match (v1, v2) {
23+
(ty::Invariant, _) | (_, ty::Invariant) => ty::Invariant,
24+
25+
(ty::Covariant, ty::Contravariant) => ty::Invariant,
26+
(ty::Contravariant, ty::Covariant) => ty::Invariant,
27+
28+
(ty::Covariant, ty::Covariant) => ty::Covariant,
29+
30+
(ty::Contravariant, ty::Contravariant) => ty::Contravariant,
31+
32+
(x, ty::Bivariant) | (ty::Bivariant, x) => x,
33+
}
34+
}
1735
struct SolveContext<'a, 'tcx> {
1836
terms_cx: TermsContext<'a, 'tcx>,
1937
constraints: Vec<Constraint<'a>>,

‎compiler/rustc_hir_analysis/src/variance/xform.rs

Lines changed: 0 additions & 22 deletions
This file was deleted.

‎compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1261,7 +1261,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12611261

12621262
fn provided_kind(
12631263
&mut self,
1264-
_preceding_args: &[ty::GenericArg<'tcx>],
12651264
param: &ty::GenericParamDef,
12661265
arg: &GenericArg<'tcx>,
12671266
) -> ty::GenericArg<'tcx> {

‎compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ use crate::{CoroutineTypes, Diverges, EnclosingBreakables, TypeckRootCtxt};
3131
/// functions, closures, and `const`s, including performing type inference
3232
/// with [`InferCtxt`].
3333
///
34-
/// This is in contrast to [`ItemCtxt`], which is used to type-check item *signatures*
35-
/// and thus does not perform type inference.
34+
/// This is in contrast to `rustc_hir_analysis::collect::ItemCtxt`, which is
35+
/// used to type-check item *signatures* and thus does not perform type
36+
/// inference.
3637
///
37-
/// See [`ItemCtxt`]'s docs for more.
38+
/// See `ItemCtxt`'s docs for more.
3839
///
39-
/// [`ItemCtxt`]: rustc_hir_analysis::collect::ItemCtxt
4040
/// [`InferCtxt`]: infer::InferCtxt
4141
pub(crate) struct FnCtxt<'a, 'tcx> {
4242
pub(super) body_id: LocalDefId,

‎compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use rustc_hir::{
1212
GenericBound, HirId, Node, PatExpr, PatExprKind, Path, QPath, Stmt, StmtKind, TyKind,
1313
WherePredicateKind, expr_needs_parens,
1414
};
15-
use rustc_hir_analysis::collect::suggest_impl_trait;
1615
use rustc_hir_analysis::hir_ty_lowering::HirTyLowerer;
16+
use rustc_hir_analysis::suggest_impl_trait;
1717
use rustc_middle::lint::in_external_macro;
1818
use rustc_middle::middle::stability::EvalResult;
1919
use rustc_middle::span_bug;

‎compiler/rustc_hir_typeck/src/method/confirm.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,6 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
413413

414414
fn provided_kind(
415415
&mut self,
416-
_preceding_args: &[ty::GenericArg<'tcx>],
417416
param: &ty::GenericParamDef,
418417
arg: &GenericArg<'tcx>,
419418
) -> ty::GenericArg<'tcx> {

0 commit comments

Comments
 (0)
Please sign in to comment.