Skip to content

Commit fdf3b31

Browse files
committed
pass the right ParamEnv to might_permit_raw_init_strict
1 parent f7eefec commit fdf3b31

File tree

3 files changed

+36
-19
lines changed

3 files changed

+36
-19
lines changed

compiler/rustc_const_eval/src/util/check_validity_requirement.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_middle::bug;
22
use rustc_middle::ty::layout::{LayoutCx, LayoutError, LayoutOf, TyAndLayout, ValidityRequirement};
3-
use rustc_middle::ty::{ParamEnv, ParamEnvAnd, Ty, TyCtxt};
3+
use rustc_middle::ty::{ParamEnvAnd, Ty, TyCtxt};
44
use rustc_target::abi::{Abi, FieldsShape, Scalar, Variants};
55

66
use crate::const_eval::{CanAccessMutGlobal, CheckAlignment, CompileTimeMachine};
@@ -30,10 +30,10 @@ pub fn check_validity_requirement<'tcx>(
3030
return Ok(!layout.abi.is_uninhabited());
3131
}
3232

33+
let layout_cx = LayoutCx { tcx, param_env: param_env_and_ty.param_env };
3334
if kind == ValidityRequirement::Uninit || tcx.sess.opts.unstable_opts.strict_init_checks {
34-
might_permit_raw_init_strict(layout, tcx, kind)
35+
might_permit_raw_init_strict(layout, &layout_cx, kind)
3536
} else {
36-
let layout_cx = LayoutCx { tcx, param_env: param_env_and_ty.param_env };
3737
might_permit_raw_init_lax(layout, &layout_cx, kind)
3838
}
3939
}
@@ -42,12 +42,12 @@ pub fn check_validity_requirement<'tcx>(
4242
/// details.
4343
fn might_permit_raw_init_strict<'tcx>(
4444
ty: TyAndLayout<'tcx>,
45-
tcx: TyCtxt<'tcx>,
45+
cx: &LayoutCx<'tcx, TyCtxt<'tcx>>,
4646
kind: ValidityRequirement,
4747
) -> Result<bool, &'tcx LayoutError<'tcx>> {
4848
let machine = CompileTimeMachine::new(CanAccessMutGlobal::No, CheckAlignment::Error);
4949

50-
let mut cx = InterpCx::new(tcx, rustc_span::DUMMY_SP, ParamEnv::reveal_all(), machine);
50+
let mut cx = InterpCx::new(cx.tcx, rustc_span::DUMMY_SP, cx.param_env, machine);
5151

5252
let allocated = cx
5353
.allocate(ty, MemoryKind::Machine(crate::const_eval::MemoryKind::Heap))

src/tools/clippy/tests/ui/uninit_vec.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![warn(clippy::uninit_vec)]
22

33
use std::mem::MaybeUninit;
4+
use std::cell::UnsafeCell;
45

56
#[derive(Default)]
67
struct MyVec {
@@ -12,6 +13,12 @@ union MyOwnMaybeUninit {
1213
uninit: (),
1314
}
1415

16+
// https://github.com/rust-lang/rust/issues/119620
17+
unsafe fn requires_paramenv<S>() {
18+
let mut vec = Vec::<UnsafeCell<*mut S>>::with_capacity(1);
19+
vec.set_len(1);
20+
}
21+
1522
fn main() {
1623
// with_capacity() -> set_len() should be detected
1724
let mut vec: Vec<u8> = Vec::with_capacity(1000);

src/tools/clippy/tests/ui/uninit_vec.stderr

+24-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
error: calling `set_len()` immediately after reserving a buffer creates uninitialized values
2-
--> tests/ui/uninit_vec.rs:17:5
2+
--> tests/ui/uninit_vec.rs:18:5
3+
|
4+
LL | let mut vec = Vec::<UnsafeCell<*mut S>>::with_capacity(1);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
LL | vec.set_len(1);
7+
| ^^^^^^^^^^^^^^
8+
|
9+
= help: initialize the buffer or wrap the content in `MaybeUninit`
10+
= note: `-D clippy::uninit-vec` implied by `-D warnings`
11+
= help: to override `-D warnings` add `#[allow(clippy::uninit_vec)]`
12+
13+
error: calling `set_len()` immediately after reserving a buffer creates uninitialized values
14+
--> tests/ui/uninit_vec.rs:24:5
315
|
416
LL | let mut vec: Vec<u8> = Vec::with_capacity(1000);
517
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -8,11 +20,9 @@ LL | vec.set_len(200);
820
| ^^^^^^^^^^^^^^^^
921
|
1022
= help: initialize the buffer or wrap the content in `MaybeUninit`
11-
= note: `-D clippy::uninit-vec` implied by `-D warnings`
12-
= help: to override `-D warnings` add `#[allow(clippy::uninit_vec)]`
1323

1424
error: calling `set_len()` immediately after reserving a buffer creates uninitialized values
15-
--> tests/ui/uninit_vec.rs:24:5
25+
--> tests/ui/uninit_vec.rs:31:5
1626
|
1727
LL | vec.reserve(1000);
1828
| ^^^^^^^^^^^^^^^^^^
@@ -23,7 +33,7 @@ LL | vec.set_len(200);
2333
= help: initialize the buffer or wrap the content in `MaybeUninit`
2434

2535
error: calling `set_len()` on empty `Vec` creates out-of-bound values
26-
--> tests/ui/uninit_vec.rs:31:5
36+
--> tests/ui/uninit_vec.rs:38:5
2737
|
2838
LL | let mut vec: Vec<u8> = Vec::new();
2939
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -32,7 +42,7 @@ LL | vec.set_len(200);
3242
| ^^^^^^^^^^^^^^^^
3343

3444
error: calling `set_len()` on empty `Vec` creates out-of-bound values
35-
--> tests/ui/uninit_vec.rs:38:5
45+
--> tests/ui/uninit_vec.rs:45:5
3646
|
3747
LL | let mut vec: Vec<u8> = Default::default();
3848
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -41,7 +51,7 @@ LL | vec.set_len(200);
4151
| ^^^^^^^^^^^^^^^^
4252

4353
error: calling `set_len()` on empty `Vec` creates out-of-bound values
44-
--> tests/ui/uninit_vec.rs:44:5
54+
--> tests/ui/uninit_vec.rs:51:5
4555
|
4656
LL | let mut vec: Vec<u8> = Vec::default();
4757
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -50,7 +60,7 @@ LL | vec.set_len(200);
5060
| ^^^^^^^^^^^^^^^^
5161

5262
error: calling `set_len()` immediately after reserving a buffer creates uninitialized values
53-
--> tests/ui/uninit_vec.rs:61:5
63+
--> tests/ui/uninit_vec.rs:68:5
5464
|
5565
LL | let mut vec: Vec<u8> = Vec::with_capacity(1000);
5666
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -61,7 +71,7 @@ LL | vec.set_len(200);
6171
= help: initialize the buffer or wrap the content in `MaybeUninit`
6272

6373
error: calling `set_len()` immediately after reserving a buffer creates uninitialized values
64-
--> tests/ui/uninit_vec.rs:71:5
74+
--> tests/ui/uninit_vec.rs:78:5
6575
|
6676
LL | my_vec.vec.reserve(1000);
6777
| ^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -72,7 +82,7 @@ LL | my_vec.vec.set_len(200);
7282
= help: initialize the buffer or wrap the content in `MaybeUninit`
7383

7484
error: calling `set_len()` immediately after reserving a buffer creates uninitialized values
75-
--> tests/ui/uninit_vec.rs:77:5
85+
--> tests/ui/uninit_vec.rs:84:5
7686
|
7787
LL | my_vec.vec = Vec::with_capacity(1000);
7888
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -83,7 +93,7 @@ LL | my_vec.vec.set_len(200);
8393
= help: initialize the buffer or wrap the content in `MaybeUninit`
8494

8595
error: calling `set_len()` immediately after reserving a buffer creates uninitialized values
86-
--> tests/ui/uninit_vec.rs:52:9
96+
--> tests/ui/uninit_vec.rs:59:9
8797
|
8898
LL | let mut vec: Vec<u8> = Vec::with_capacity(1000);
8999
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -94,7 +104,7 @@ LL | vec.set_len(200);
94104
= help: initialize the buffer or wrap the content in `MaybeUninit`
95105

96106
error: calling `set_len()` immediately after reserving a buffer creates uninitialized values
97-
--> tests/ui/uninit_vec.rs:56:9
107+
--> tests/ui/uninit_vec.rs:63:9
98108
|
99109
LL | vec.reserve(1000);
100110
| ^^^^^^^^^^^^^^^^^^
@@ -105,7 +115,7 @@ LL | vec.set_len(200);
105115
= help: initialize the buffer or wrap the content in `MaybeUninit`
106116

107117
error: calling `set_len()` immediately after reserving a buffer creates uninitialized values
108-
--> tests/ui/uninit_vec.rs:132:9
118+
--> tests/ui/uninit_vec.rs:139:9
109119
|
110120
LL | let mut vec: Vec<T> = Vec::with_capacity(1000);
111121
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -115,5 +125,5 @@ LL | vec.set_len(10);
115125
|
116126
= help: initialize the buffer or wrap the content in `MaybeUninit`
117127

118-
error: aborting due to 11 previous errors
128+
error: aborting due to 12 previous errors
119129

0 commit comments

Comments
 (0)