Skip to content

Commit bb93c23

Browse files
committed
use TypingEnv when no infcx is available
the behavior of the type system not only depends on the current assumptions, but also the currentnphase of the compiler. This is mostly necessary as we need to decide whether and how to reveal opaque types. We track this via the `TypingMode`.
1 parent 1ceaa90 commit bb93c23

33 files changed

+83
-79
lines changed

clippy_lints/src/assigning_clones.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ impl<'tcx> LateLintPass<'tcx> for AssigningClones {
9696
},
9797
_ => return,
9898
}
99-
&& let Ok(Some(resolved_fn)) = Instance::try_resolve(cx.tcx, cx.param_env, fn_id, fn_gen_args)
99+
&& let Ok(Some(resolved_fn)) = Instance::try_resolve(cx.tcx, cx.typing_env(), fn_id, fn_gen_args)
100100
// TODO: This check currently bails if the local variable has no initializer.
101101
// That is overly conservative - the lint should fire even if there was no initializer,
102102
// but the variable has been initialized before `lhs` was evaluated.

clippy_lints/src/bool_assert_comparison.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ fn is_impl_not_trait_with_bool_out<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -
6262
})
6363
.is_some_and(|assoc_item| {
6464
let proj = Ty::new_projection(cx.tcx, assoc_item.def_id, cx.tcx.mk_args_trait(ty, []));
65-
let nty = cx.tcx.normalize_erasing_regions(cx.param_env, proj);
65+
let nty = cx.tcx.normalize_erasing_regions(cx.typing_env(), proj);
6666

6767
nty.is_bool()
6868
})

clippy_lints/src/dereference.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_hir::{
1717
};
1818
use rustc_lint::{LateContext, LateLintPass};
1919
use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow, AutoBorrowMutability};
20-
use rustc_middle::ty::{self, ParamEnv, Ty, TyCtxt, TypeVisitableExt, TypeckResults};
20+
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt, TypeckResults};
2121
use rustc_session::impl_lint_pass;
2222
use rustc_span::symbol::sym;
2323
use rustc_span::{Span, Symbol};
@@ -755,7 +755,8 @@ impl TyCoercionStability {
755755
DefinedTy::Hir(ty) => Self::for_hir_ty(ty),
756756
DefinedTy::Mir(ty) => Self::for_mir_ty(
757757
cx.tcx,
758-
ty.param_env,
758+
// FIXME(#132279): convert `DefinedTy` to use `TypingEnv` instead.
759+
ty::TypingEnv::from_param_env(ty.param_env),
759760
cx.tcx.instantiate_bound_regions_with_erased(ty.value),
760761
for_return,
761762
),
@@ -823,12 +824,12 @@ impl TyCoercionStability {
823824
}
824825
}
825826

826-
fn for_mir_ty<'tcx>(tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>, ty: Ty<'tcx>, for_return: bool) -> Self {
827+
fn for_mir_ty<'tcx>(tcx: TyCtxt<'tcx>, typing_env: ty::TypingEnv<'tcx>, ty: Ty<'tcx>, for_return: bool) -> Self {
827828
let ty::Ref(_, mut ty, _) = *ty.kind() else {
828829
return Self::None;
829830
};
830831

831-
ty = tcx.try_normalize_erasing_regions(param_env, ty).unwrap_or(ty);
832+
ty = tcx.try_normalize_erasing_regions(typing_env, ty).unwrap_or(ty);
832833
loop {
833834
break match *ty.kind() {
834835
ty::Ref(_, ref_ty, _) => {

clippy_lints/src/drop_forget_ref.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,15 @@ impl<'tcx> LateLintPass<'tcx> for DropForgetRef {
9999
sym::mem_forget if is_copy => return,
100100
sym::mem_drop if is_type_lang_item(cx, arg_ty, LangItem::ManuallyDrop) => return,
101101
sym::mem_drop
102-
if !(arg_ty.needs_drop(cx.tcx, cx.param_env)
102+
if !(arg_ty.needs_drop(cx.tcx, cx.typing_env())
103103
|| is_must_use_func_call(cx, arg)
104104
|| is_must_use_ty(cx, arg_ty)
105105
|| drop_is_single_call_in_arm) =>
106106
{
107107
(DROP_NON_DROP, DROP_NON_DROP_SUMMARY.into(), Some(arg.span))
108108
},
109109
sym::mem_forget => {
110-
if arg_ty.needs_drop(cx.tcx, cx.param_env) {
110+
if arg_ty.needs_drop(cx.tcx, cx.typing_env()) {
111111
(
112112
MEM_FORGET,
113113
Cow::Owned(format!(

clippy_lints/src/iter_not_returning_iterator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ fn check_sig(cx: &LateContext<'_>, name: Symbol, sig: &FnSig<'_>, fn_id: LocalDe
7070
.instantiate_bound_regions_with_erased(cx.tcx.fn_sig(fn_id).instantiate_identity().output());
7171
let ret_ty = cx
7272
.tcx
73-
.try_normalize_erasing_regions(cx.param_env, ret_ty)
73+
.try_normalize_erasing_regions(cx.typing_env(), ret_ty)
7474
.unwrap_or(ret_ty);
7575
if cx
7676
.tcx

clippy_lints/src/iter_without_into_iter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ impl {self_ty_without_ref} {{
215215
&& implements_trait(cx, ret_ty, iterator_did, &[])
216216
&& let Some(iter_ty) = make_normalized_projection(
217217
cx.tcx,
218-
cx.param_env,
218+
cx.typing_env(),
219219
iterator_did,
220220
sym::Item,
221221
[ret_ty],

clippy_lints/src/large_const_arrays.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_errors::Applicability;
44
use rustc_hir::{Item, ItemKind};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_middle::ty::layout::LayoutOf;
7-
use rustc_middle::ty::{self, ParamEnv};
7+
use rustc_middle::ty;
88
use rustc_session::impl_lint_pass;
99
use rustc_span::{BytePos, Pos, Span};
1010

@@ -57,7 +57,7 @@ impl<'tcx> LateLintPass<'tcx> for LargeConstArrays {
5757
&& let ty = cx.tcx.type_of(item.owner_id).instantiate_identity()
5858
&& let ty::Array(element_type, cst) = ty.kind()
5959
&& let Some((ty::ValTree::Leaf(element_count), _)) = cx.tcx
60-
.try_normalize_erasing_regions(ParamEnv::empty(), *cst).unwrap_or(*cst).try_to_valtree()
60+
.try_normalize_erasing_regions(cx.typing_env(), *cst).unwrap_or(*cst).try_to_valtree()
6161
&& let element_count = element_count.to_target_usize(cx.tcx)
6262
&& let Ok(element_size) = cx.layout_of(*element_type).map(|l| l.size.bytes())
6363
&& u128::from(self.maximum_allowed_size) < u128::from(element_count) * u128::from(element_size)

clippy_lints/src/large_futures.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl<'tcx> LateLintPass<'tcx> for LargeFuture {
6363
&& let ty = cx.typeck_results().expr_ty(arg)
6464
&& let Some(future_trait_def_id) = cx.tcx.lang_items().future_trait()
6565
&& implements_trait(cx, ty, future_trait_def_id, &[])
66-
&& let Ok(layout) = cx.tcx.layout_of(cx.param_env.and(ty))
66+
&& let Ok(layout) = cx.tcx.layout_of(cx.typing_env().as_query_input(ty))
6767
&& let size = layout.layout.size()
6868
&& size >= Size::from_bytes(self.future_size_threshold)
6969
{

clippy_lints/src/large_stack_frames.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,11 @@ impl<'tcx> LateLintPass<'tcx> for LargeStackFrames {
150150
}
151151

152152
let mir = cx.tcx.optimized_mir(def_id);
153-
let param_env = cx.tcx.param_env(def_id);
153+
let typing_env = mir.typing_env(cx.tcx);
154154

155155
let sizes_of_locals = || {
156156
mir.local_decls.iter().filter_map(|local| {
157-
let layout = cx.tcx.layout_of(param_env.and(local.ty)).ok()?;
157+
let layout = cx.tcx.layout_of(typing_env.as_query_input(local.ty)).ok()?;
158158
Some((local, layout.size.bytes()))
159159
})
160160
};

clippy_lints/src/loops/explicit_iter_loop.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ fn is_ref_iterable<'tcx>(
151151
// Using by value won't consume anything
152152
if implements_trait(cx, self_ty, trait_id, &[])
153153
&& let Some(ty) =
154-
make_normalized_projection(cx.tcx, cx.param_env, trait_id, sym!(IntoIter), [self_ty])
154+
make_normalized_projection(cx.tcx, cx.typing_env(), trait_id, sym!(IntoIter), [self_ty])
155155
&& ty == res_ty
156156
{
157157
return Some((AdjustKind::None, self_ty));
@@ -168,7 +168,7 @@ fn is_ref_iterable<'tcx>(
168168
};
169169
if implements_trait(cx, self_ty, trait_id, &[])
170170
&& let Some(ty) =
171-
make_normalized_projection(cx.tcx, cx.param_env, trait_id, sym!(IntoIter), [self_ty])
171+
make_normalized_projection(cx.tcx, cx.typing_env(), trait_id, sym!(IntoIter), [self_ty])
172172
&& ty == res_ty
173173
{
174174
return Some((AdjustKind::reborrow(mutbl), self_ty));
@@ -181,7 +181,7 @@ fn is_ref_iterable<'tcx>(
181181
// Attempt to borrow
182182
let self_ty = Ty::new_ref(cx.tcx, cx.tcx.lifetimes.re_erased, self_ty, mutbl);
183183
if implements_trait(cx, self_ty, trait_id, &[])
184-
&& let Some(ty) = make_normalized_projection(cx.tcx, cx.param_env, trait_id, sym!(IntoIter), [self_ty])
184+
&& let Some(ty) = make_normalized_projection(cx.tcx, cx.typing_env(), trait_id, sym!(IntoIter), [self_ty])
185185
&& ty == res_ty
186186
{
187187
return Some((AdjustKind::borrow(mutbl), self_ty));
@@ -204,7 +204,7 @@ fn is_ref_iterable<'tcx>(
204204
&& target != self_ty
205205
&& implements_trait(cx, target, trait_id, &[])
206206
&& let Some(ty) =
207-
make_normalized_projection(cx.tcx, cx.param_env, trait_id, sym!(IntoIter), [target])
207+
make_normalized_projection(cx.tcx, cx.typing_env(), trait_id, sym!(IntoIter), [target])
208208
&& ty == res_ty
209209
{
210210
Some((AdjustKind::auto_reborrow(mutbl), target))
@@ -222,7 +222,7 @@ fn is_ref_iterable<'tcx>(
222222
if is_copy(cx, target)
223223
&& implements_trait(cx, target, trait_id, &[])
224224
&& let Some(ty) =
225-
make_normalized_projection(cx.tcx, cx.param_env, trait_id, sym!(IntoIter), [target])
225+
make_normalized_projection(cx.tcx, cx.typing_env(), trait_id, sym!(IntoIter), [target])
226226
&& ty == res_ty
227227
{
228228
Some((AdjustKind::Deref, target))
@@ -240,7 +240,7 @@ fn is_ref_iterable<'tcx>(
240240
if self_ty.is_ref()
241241
&& implements_trait(cx, target, trait_id, &[])
242242
&& let Some(ty) =
243-
make_normalized_projection(cx.tcx, cx.param_env, trait_id, sym!(IntoIter), [target])
243+
make_normalized_projection(cx.tcx, cx.typing_env(), trait_id, sym!(IntoIter), [target])
244244
&& ty == res_ty
245245
{
246246
Some((AdjustKind::auto_borrow(mutbl), target))

0 commit comments

Comments
 (0)