Skip to content

Commit d667dd5

Browse files
committed
remove TypingMode::from_param_env in clippy
1 parent 4813fda commit d667dd5

File tree

7 files changed

+77
-68
lines changed

7 files changed

+77
-68
lines changed

Diff for: src/tools/clippy/clippy_lints/src/dereference.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rustc_hir::{
1515
self as hir, BindingMode, Body, BodyId, BorrowKind, Expr, ExprKind, HirId, MatchSource, Mutability, Node, Pat,
1616
PatKind, Path, QPath, TyKind, UnOp,
1717
};
18+
use rustc_hir::def_id::DefId;
1819
use rustc_lint::{LateContext, LateLintPass};
1920
use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow, AutoBorrowMutability};
2021
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt, TypeckResults};
@@ -753,11 +754,10 @@ impl TyCoercionStability {
753754
fn for_defined_ty<'tcx>(cx: &LateContext<'tcx>, ty: DefinedTy<'tcx>, for_return: bool) -> Self {
754755
match ty {
755756
DefinedTy::Hir(ty) => Self::for_hir_ty(ty),
756-
DefinedTy::Mir(ty) => Self::for_mir_ty(
757+
DefinedTy::Mir { def_site_def_id, ty } => Self::for_mir_ty(
757758
cx.tcx,
758-
// FIXME(#132279): convert `DefinedTy` to use `TypingEnv` instead.
759-
ty::TypingEnv::from_param_env(ty.param_env),
760-
cx.tcx.instantiate_bound_regions_with_erased(ty.value),
759+
def_site_def_id,
760+
cx.tcx.instantiate_bound_regions_with_erased(ty),
761761
for_return,
762762
),
763763
}
@@ -824,12 +824,15 @@ impl TyCoercionStability {
824824
}
825825
}
826826

827-
fn for_mir_ty<'tcx>(tcx: TyCtxt<'tcx>, typing_env: ty::TypingEnv<'tcx>, ty: Ty<'tcx>, for_return: bool) -> Self {
827+
fn for_mir_ty<'tcx>(tcx: TyCtxt<'tcx>, def_site_def_id: Option<DefId>, ty: Ty<'tcx>, for_return: bool) -> Self {
828828
let ty::Ref(_, mut ty, _) = *ty.kind() else {
829829
return Self::None;
830830
};
831831

832-
ty = tcx.try_normalize_erasing_regions(typing_env, ty).unwrap_or(ty);
832+
if let Some(def_id) = def_site_def_id {
833+
let typing_env = ty::TypingEnv::non_body_analysis(tcx, def_id);
834+
ty = tcx.try_normalize_erasing_regions(typing_env, ty).unwrap_or(ty);
835+
}
833836
loop {
834837
break match *ty.kind() {
835838
ty::Ref(_, ref_ty, _) => {

Diff for: src/tools/clippy/clippy_lints/src/derive.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -454,13 +454,13 @@ fn check_partial_eq_without_eq<'tcx>(cx: &LateContext<'tcx>, span: Span, trait_r
454454
&& cx.tcx.is_diagnostic_item(sym::PartialEq, def_id)
455455
&& !has_non_exhaustive_attr(cx.tcx, *adt)
456456
&& !ty_implements_eq_trait(cx.tcx, ty, eq_trait_def_id)
457-
&& let param_env = param_env_for_derived_eq(cx.tcx, adt.did(), eq_trait_def_id)
457+
&& let typing_env = typing_env_env_for_derived_eq(cx.tcx, adt.did(), eq_trait_def_id)
458458
&& let Some(local_def_id) = adt.did().as_local()
459459
// If all of our fields implement `Eq`, we can implement `Eq` too
460460
&& adt
461461
.all_fields()
462462
.map(|f| f.ty(cx.tcx, args))
463-
.all(|ty| implements_trait_with_env(cx.tcx, param_env, ty, eq_trait_def_id, None, &[]))
463+
.all(|ty| implements_trait_with_env(cx.tcx, typing_env, ty, eq_trait_def_id, None, &[]))
464464
{
465465
span_lint_hir_and_then(
466466
cx,
@@ -485,7 +485,7 @@ fn ty_implements_eq_trait<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, eq_trait_id: De
485485
}
486486

487487
/// Creates the `ParamEnv` used for the give type's derived `Eq` impl.
488-
fn param_env_for_derived_eq(tcx: TyCtxt<'_>, did: DefId, eq_trait_id: DefId) -> ParamEnv<'_> {
488+
fn typing_env_env_for_derived_eq(tcx: TyCtxt<'_>, did: DefId, eq_trait_id: DefId) -> ty::TypingEnv<'_> {
489489
// Initial map from generic index to param def.
490490
// Vec<(param_def, needs_eq)>
491491
let mut params = tcx
@@ -506,7 +506,7 @@ fn param_env_for_derived_eq(tcx: TyCtxt<'_>, did: DefId, eq_trait_id: DefId) ->
506506
}
507507
}
508508

509-
ParamEnv::new(
509+
let param_env = ParamEnv::new(
510510
tcx.mk_clauses_from_iter(ty_predicates.iter().map(|&(p, _)| p).chain(
511511
params.iter().filter(|&&(_, needs_eq)| needs_eq).map(|&(param, _)| {
512512
ClauseKind::Trait(TraitPredicate {
@@ -517,5 +517,9 @@ fn param_env_for_derived_eq(tcx: TyCtxt<'_>, did: DefId, eq_trait_id: DefId) ->
517517
}),
518518
)),
519519
Reveal::UserFacing,
520-
)
520+
);
521+
ty::TypingEnv {
522+
typing_mode: ty::TypingMode::non_body_analysis(),
523+
param_env,
524+
}
521525
}

Diff for: src/tools/clippy/clippy_lints/src/loops/explicit_iter_loop.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,11 @@ fn is_ref_iterable<'tcx>(
115115
.tcx
116116
.liberate_late_bound_regions(fn_id, cx.tcx.fn_sig(fn_id).skip_binder())
117117
&& let &[req_self_ty, req_res_ty] = &**sig.inputs_and_output
118-
&& let param_env = cx.tcx.param_env(fn_id)
119-
&& implements_trait_with_env(cx.tcx, param_env, req_self_ty, trait_id, Some(fn_id), &[])
118+
&& let typing_env = ty::TypingEnv::non_body_analysis(cx.tcx, fn_id)
119+
&& implements_trait_with_env(cx.tcx, typing_env, req_self_ty, trait_id, Some(fn_id), &[])
120120
&& let Some(into_iter_ty) =
121-
make_normalized_projection_with_regions(cx.tcx, param_env, trait_id, sym!(IntoIter), [req_self_ty])
122-
&& let req_res_ty = normalize_with_regions(cx.tcx, param_env, req_res_ty)
121+
make_normalized_projection_with_regions(cx.tcx, typing_env, trait_id, sym!(IntoIter), [req_self_ty])
122+
&& let req_res_ty = normalize_with_regions(cx.tcx, typing_env, req_res_ty)
123123
&& into_iter_ty == req_res_ty
124124
{
125125
let adjustments = typeck.expr_adjustments(self_arg);

Diff for: src/tools/clippy/clippy_lints/src/needless_borrows_for_generic_args.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessBorrowsForGenericArgs<'tcx> {
8585
&& use_cx.same_ctxt
8686
&& !use_cx.is_ty_unified
8787
&& let use_node = use_cx.use_node(cx)
88-
&& let Some(DefinedTy::Mir(ty)) = use_node.defined_ty(cx)
89-
&& let ty::Param(ty) = *ty.value.skip_binder().kind()
88+
&& let Some(DefinedTy::Mir { def_site_def_id: _, ty }) = use_node.defined_ty(cx)
89+
&& let ty::Param(param_ty) = *ty.skip_binder().kind()
9090
&& let Some((hir_id, fn_id, i)) = match use_node {
9191
ExprUseNode::MethodArg(_, _, 0) => None,
9292
ExprUseNode::MethodArg(hir_id, None, i) => cx
@@ -112,7 +112,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessBorrowsForGenericArgs<'tcx> {
112112
fn_id,
113113
cx.typeck_results().node_args(hir_id),
114114
i,
115-
ty,
115+
param_ty,
116116
expr,
117117
&self.msrv,
118118
)

Diff for: src/tools/clippy/clippy_lints/src/needless_pass_by_value.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,14 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
182182
&& !is_copy(cx, ty)
183183
&& ty.is_sized(cx.tcx, cx.typing_env())
184184
&& !allowed_traits.iter().any(|&t| {
185-
implements_trait_with_env_from_iter(cx.tcx, cx.param_env, ty, t, None, [Option::<
186-
ty::GenericArg<'tcx>,
187-
>::None])
185+
implements_trait_with_env_from_iter(
186+
cx.tcx,
187+
cx.typing_env(),
188+
ty,
189+
t,
190+
None,
191+
[None::<ty::GenericArg<'tcx>>]
192+
)
188193
})
189194
&& !implements_borrow_trait
190195
&& !all_borrowable_trait

Diff for: src/tools/clippy/clippy_utils/src/lib.rs

+30-24
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow};
116116
use rustc_middle::ty::fast_reject::SimplifiedType;
117117
use rustc_middle::ty::layout::IntegerExt;
118118
use rustc_middle::ty::{
119-
self as rustc_ty, Binder, BorrowKind, ClosureKind, EarlyBinder, FloatTy, GenericArgsRef, IntTy, ParamEnv,
120-
ParamEnvAnd, Ty, TyCtxt, TypeVisitableExt, UintTy, UpvarCapture,
119+
self as rustc_ty, Binder, BorrowKind, ClosureKind, EarlyBinder, FloatTy, GenericArgsRef, IntTy,
120+
Ty, TyCtxt, TypeVisitableExt, UintTy, UpvarCapture,
121121
};
122122
use rustc_span::hygiene::{ExpnKind, MacroKind};
123123
use rustc_span::source_map::SourceMap;
@@ -2712,9 +2712,17 @@ pub fn walk_to_expr_usage<'tcx, T>(
27122712
pub enum DefinedTy<'tcx> {
27132713
// Used for locals and closures defined within the function.
27142714
Hir(&'tcx hir::Ty<'tcx>),
2715-
/// Used for function signatures, and constant and static values. This includes the `ParamEnv`
2716-
/// from the definition site.
2717-
Mir(ParamEnvAnd<'tcx, Binder<'tcx, Ty<'tcx>>>),
2715+
/// Used for function signatures, and constant and static values. The type is
2716+
/// in the context of its definition site. We also track the `def_id` of its
2717+
/// definition site.
2718+
///
2719+
/// WARNING: As the `ty` in in the scope of the definition, not of the function
2720+
/// using it, you must be very careful with how you use it. Using it in the wrong
2721+
/// scope easily results in ICEs.
2722+
Mir {
2723+
def_site_def_id: Option<DefId>,
2724+
ty: Binder<'tcx, Ty<'tcx>>,
2725+
},
27182726
}
27192727

27202728
/// The context an expressions value is used in.
@@ -2833,10 +2841,10 @@ impl<'tcx> ExprUseNode<'tcx> {
28332841
pub fn defined_ty(&self, cx: &LateContext<'tcx>) -> Option<DefinedTy<'tcx>> {
28342842
match *self {
28352843
Self::LetStmt(LetStmt { ty: Some(ty), .. }) => Some(DefinedTy::Hir(ty)),
2836-
Self::ConstStatic(id) => Some(DefinedTy::Mir(
2837-
cx.param_env
2838-
.and(Binder::dummy(cx.tcx.type_of(id).instantiate_identity())),
2839-
)),
2844+
Self::ConstStatic(id) => Some(DefinedTy::Mir {
2845+
def_site_def_id: Some(id.def_id.to_def_id()),
2846+
ty: Binder::dummy(cx.tcx.type_of(id).instantiate_identity()),
2847+
}),
28402848
Self::Return(id) => {
28412849
if let Node::Expr(Expr {
28422850
kind: ExprKind::Closure(c),
@@ -2848,9 +2856,8 @@ impl<'tcx> ExprUseNode<'tcx> {
28482856
FnRetTy::Return(ty) => Some(DefinedTy::Hir(ty)),
28492857
}
28502858
} else {
2851-
Some(DefinedTy::Mir(
2852-
cx.param_env.and(cx.tcx.fn_sig(id).instantiate_identity().output()),
2853-
))
2859+
let ty = cx.tcx.fn_sig(id).instantiate_identity().output();
2860+
Some(DefinedTy::Mir { def_site_def_id: Some(id.def_id.to_def_id()), ty })
28542861
}
28552862
},
28562863
Self::Field(field) => match get_parent_expr_for_hir(cx, field.hir_id) {
@@ -2866,12 +2873,9 @@ impl<'tcx> ExprUseNode<'tcx> {
28662873
.find(|f| f.name == field.ident.name)
28672874
.map(|f| (adt, f))
28682875
})
2869-
.map(|(adt, field_def)| {
2870-
DefinedTy::Mir(
2871-
cx.tcx
2872-
.param_env(adt.did())
2873-
.and(Binder::dummy(cx.tcx.type_of(field_def.did).instantiate_identity())),
2874-
)
2876+
.map(|(adt, field_def)| DefinedTy::Mir {
2877+
def_site_def_id: Some(adt.did()),
2878+
ty: Binder::dummy(cx.tcx.type_of(field_def.did).instantiate_identity()),
28752879
}),
28762880
_ => None,
28772881
},
@@ -2880,17 +2884,19 @@ impl<'tcx> ExprUseNode<'tcx> {
28802884
let (hir_ty, ty) = sig.input_with_hir(i)?;
28812885
Some(match hir_ty {
28822886
Some(hir_ty) => DefinedTy::Hir(hir_ty),
2883-
None => DefinedTy::Mir(
2884-
sig.predicates_id()
2885-
.map_or(ParamEnv::empty(), |id| cx.tcx.param_env(id))
2886-
.and(ty),
2887-
),
2887+
None => DefinedTy::Mir {
2888+
def_site_def_id: sig.predicates_id(),
2889+
ty,
2890+
}
28882891
})
28892892
},
28902893
Self::MethodArg(id, _, i) => {
28912894
let id = cx.typeck_results().type_dependent_def_id(id)?;
28922895
let sig = cx.tcx.fn_sig(id).skip_binder();
2893-
Some(DefinedTy::Mir(cx.tcx.param_env(id).and(sig.input(i))))
2896+
Some(DefinedTy::Mir {
2897+
def_site_def_id: Some(id),
2898+
ty: sig.input(i),
2899+
})
28942900
},
28952901
Self::LetStmt(_) | Self::FieldAccess(..) | Self::Callee | Self::Other | Self::AddrOf(..) => None,
28962902
}

Diff for: src/tools/clippy/clippy_utils/src/ty.rs

+14-23
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_middle::ty::layout::ValidityRequirement;
1919
use rustc_middle::ty::{
2020
self, AdtDef, AliasTy, AssocItem, AssocKind, Binder, BoundRegion, FnSig, GenericArg, GenericArgKind,
2121
GenericArgsRef, GenericParamDefKind, IntTy, ParamEnv, Region, RegionKind, TraitRef, Ty, TyCtxt, TypeSuperVisitable,
22-
TypeVisitable, TypeVisitableExt, TypeVisitor, TypingMode, UintTy, Upcast, VariantDef, VariantDiscr,
22+
TypeVisitable, TypeVisitableExt, TypeVisitor, UintTy, Upcast, VariantDef, VariantDiscr,
2323
};
2424
use rustc_span::symbol::Ident;
2525
use rustc_span::{DUMMY_SP, Span, Symbol, sym};
@@ -226,7 +226,7 @@ pub fn implements_trait<'tcx>(
226226
trait_id: DefId,
227227
args: &[GenericArg<'tcx>],
228228
) -> bool {
229-
implements_trait_with_env_from_iter(cx.tcx, cx.param_env, ty, trait_id, None, args.iter().map(|&x| Some(x)))
229+
implements_trait_with_env_from_iter(cx.tcx, cx.typing_env(), ty, trait_id, None, args.iter().map(|&x| Some(x)))
230230
}
231231

232232
/// Same as `implements_trait` but allows using a `ParamEnv` different from the lint context.
@@ -235,19 +235,19 @@ pub fn implements_trait<'tcx>(
235235
/// environment, used for checking const traits.
236236
pub fn implements_trait_with_env<'tcx>(
237237
tcx: TyCtxt<'tcx>,
238-
param_env: ParamEnv<'tcx>,
238+
typing_env: ty::TypingEnv<'tcx>,
239239
ty: Ty<'tcx>,
240240
trait_id: DefId,
241241
callee_id: Option<DefId>,
242242
args: &[GenericArg<'tcx>],
243243
) -> bool {
244-
implements_trait_with_env_from_iter(tcx, param_env, ty, trait_id, callee_id, args.iter().map(|&x| Some(x)))
244+
implements_trait_with_env_from_iter(tcx, typing_env, ty, trait_id, callee_id, args.iter().map(|&x| Some(x)))
245245
}
246246

247247
/// Same as `implements_trait_from_env` but takes the arguments as an iterator.
248248
pub fn implements_trait_with_env_from_iter<'tcx>(
249249
tcx: TyCtxt<'tcx>,
250-
param_env: ParamEnv<'tcx>,
250+
typing_env: ty::TypingEnv<'tcx>,
251251
ty: Ty<'tcx>,
252252
trait_id: DefId,
253253
callee_id: Option<DefId>,
@@ -268,7 +268,7 @@ pub fn implements_trait_with_env_from_iter<'tcx>(
268268
return false;
269269
}
270270

271-
let infcx = tcx.infer_ctxt().build(TypingMode::from_param_env(param_env));
271+
let (infcx, param_env) = tcx.infer_ctxt().build_with_typing_env(typing_env);
272272
let args = args
273273
.into_iter()
274274
.map(|arg| arg.into().unwrap_or_else(|| infcx.next_ty_var(DUMMY_SP).into()))
@@ -1239,12 +1239,12 @@ impl<'tcx> InteriorMut<'tcx> {
12391239

12401240
pub fn make_normalized_projection_with_regions<'tcx>(
12411241
tcx: TyCtxt<'tcx>,
1242-
param_env: ParamEnv<'tcx>,
1242+
typing_env: ty::TypingEnv<'tcx>,
12431243
container_id: DefId,
12441244
assoc_ty: Symbol,
12451245
args: impl IntoIterator<Item = impl Into<GenericArg<'tcx>>>,
12461246
) -> Option<Ty<'tcx>> {
1247-
fn helper<'tcx>(tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>, ty: AliasTy<'tcx>) -> Option<Ty<'tcx>> {
1247+
fn helper<'tcx>(tcx: TyCtxt<'tcx>, typing_env: ty::TypingEnv<'tcx>, ty: AliasTy<'tcx>) -> Option<Ty<'tcx>> {
12481248
#[cfg(debug_assertions)]
12491249
if let Some((i, arg)) = ty
12501250
.args
@@ -1261,10 +1261,8 @@ pub fn make_normalized_projection_with_regions<'tcx>(
12611261
return None;
12621262
}
12631263
let cause = ObligationCause::dummy();
1264-
match tcx
1265-
.infer_ctxt()
1266-
.build(TypingMode::from_param_env(param_env))
1267-
.at(&cause, param_env)
1264+
let (infcx, param_env) = tcx.infer_ctxt().build_with_typing_env(typing_env);
1265+
match infcx.at(&cause, param_env)
12681266
.query_normalize(Ty::new_projection_from_args(tcx, ty.def_id, ty.args))
12691267
{
12701268
Ok(ty) => Some(ty.value),
@@ -1274,20 +1272,13 @@ pub fn make_normalized_projection_with_regions<'tcx>(
12741272
},
12751273
}
12761274
}
1277-
helper(tcx, param_env, make_projection(tcx, container_id, assoc_ty, args)?)
1275+
helper(tcx, typing_env, make_projection(tcx, container_id, assoc_ty, args)?)
12781276
}
12791277

1280-
pub fn normalize_with_regions<'tcx>(tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> {
1278+
pub fn normalize_with_regions<'tcx>(tcx: TyCtxt<'tcx>, typing_env: ty::TypingEnv<'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> {
12811279
let cause = ObligationCause::dummy();
1282-
match tcx
1283-
.infer_ctxt()
1284-
.build(TypingMode::from_param_env(param_env))
1285-
.at(&cause, param_env)
1286-
.query_normalize(ty)
1287-
{
1288-
Ok(ty) => ty.value,
1289-
Err(_) => ty,
1290-
}
1280+
let (infcx, param_env) = tcx.infer_ctxt().build_with_typing_env(typing_env);
1281+
infcx.at(&cause, param_env).query_normalize(ty).map_or(ty, |ty| ty.value)
12911282
}
12921283

12931284
/// Checks if the type is `core::mem::ManuallyDrop<_>`

0 commit comments

Comments
 (0)