Skip to content

Commit a719718

Browse files
committed
Auto merge of #96425 - oli-obk:fix_incremental_regression_unsafety_checking, r=compiler-errors
Fix incremental perf regression unsafety checking Perf regression introduced in #96294 We will simply avoid emitting the name of the unsafe function in MIR unsafeck, since we're moving to THIR unsafeck anyway.
2 parents 082e4ca + 3568bdc commit a719718

29 files changed

+171
-167
lines changed

compiler/rustc_middle/src/mir/query.rs

+19-49
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use rustc_index::vec::IndexVec;
1212
use rustc_span::Span;
1313
use rustc_target::abi::VariantIdx;
1414
use smallvec::SmallVec;
15-
use std::borrow::Cow;
1615
use std::cell::Cell;
1716
use std::fmt::{self, Debug};
1817

@@ -29,7 +28,7 @@ pub enum UnsafetyViolationKind {
2928

3029
#[derive(Copy, Clone, PartialEq, TyEncodable, TyDecodable, HashStable, Debug)]
3130
pub enum UnsafetyViolationDetails {
32-
CallToUnsafeFunction(Option<DefId>),
31+
CallToUnsafeFunction,
3332
UseOfInlineAssembly,
3433
InitializingTypeWith,
3534
CastOfPointerToInt,
@@ -40,95 +39,66 @@ pub enum UnsafetyViolationDetails {
4039
AccessToUnionField,
4140
MutationOfLayoutConstrainedField,
4241
BorrowOfLayoutConstrainedField,
43-
CallToFunctionWith(DefId),
42+
CallToFunctionWith,
4443
}
4544

4645
impl UnsafetyViolationDetails {
47-
pub fn simple_description(&self) -> &'static str {
48-
use UnsafetyViolationDetails::*;
49-
50-
match self {
51-
CallToUnsafeFunction(..) => "call to unsafe function",
52-
UseOfInlineAssembly => "use of inline assembly",
53-
InitializingTypeWith => "initializing type with `rustc_layout_scalar_valid_range` attr",
54-
CastOfPointerToInt => "cast of pointer to int",
55-
UseOfMutableStatic => "use of mutable static",
56-
UseOfExternStatic => "use of extern static",
57-
DerefOfRawPointer => "dereference of raw pointer",
58-
AssignToDroppingUnionField => "assignment to union field that might need dropping",
59-
AccessToUnionField => "access to union field",
60-
MutationOfLayoutConstrainedField => "mutation of layout constrained field",
61-
BorrowOfLayoutConstrainedField => {
62-
"borrow of layout constrained field with interior mutability"
63-
}
64-
CallToFunctionWith(..) => "call to function with `#[target_feature]`",
65-
}
66-
}
67-
68-
pub fn description_and_note(&self, tcx: TyCtxt<'_>) -> (Cow<'static, str>, &'static str) {
46+
pub fn description_and_note(&self) -> (&'static str, &'static str) {
6947
use UnsafetyViolationDetails::*;
7048
match self {
71-
CallToUnsafeFunction(did) => (
72-
if let Some(did) = did {
73-
Cow::from(format!("call to unsafe function `{}`", tcx.def_path_str(*did)))
74-
} else {
75-
Cow::Borrowed(self.simple_description())
76-
},
49+
CallToUnsafeFunction => (
50+
"call to unsafe function",
7751
"consult the function's documentation for information on how to avoid undefined \
7852
behavior",
7953
),
8054
UseOfInlineAssembly => (
81-
Cow::Borrowed(self.simple_description()),
55+
"use of inline assembly",
8256
"inline assembly is entirely unchecked and can cause undefined behavior",
8357
),
8458
InitializingTypeWith => (
85-
Cow::Borrowed(self.simple_description()),
59+
"initializing type with `rustc_layout_scalar_valid_range` attr",
8660
"initializing a layout restricted type's field with a value outside the valid \
8761
range is undefined behavior",
8862
),
89-
CastOfPointerToInt => (
90-
Cow::Borrowed(self.simple_description()),
91-
"casting pointers to integers in constants",
92-
),
63+
CastOfPointerToInt => {
64+
("cast of pointer to int", "casting pointers to integers in constants")
65+
}
9366
UseOfMutableStatic => (
94-
Cow::Borrowed(self.simple_description()),
67+
"use of mutable static",
9568
"mutable statics can be mutated by multiple threads: aliasing violations or data \
9669
races will cause undefined behavior",
9770
),
9871
UseOfExternStatic => (
99-
Cow::Borrowed(self.simple_description()),
72+
"use of extern static",
10073
"extern statics are not controlled by the Rust type system: invalid data, \
10174
aliasing violations or data races will cause undefined behavior",
10275
),
10376
DerefOfRawPointer => (
104-
Cow::Borrowed(self.simple_description()),
77+
"dereference of raw pointer",
10578
"raw pointers may be null, dangling or unaligned; they can violate aliasing rules \
10679
and cause data races: all of these are undefined behavior",
10780
),
10881
AssignToDroppingUnionField => (
109-
Cow::Borrowed(self.simple_description()),
82+
"assignment to union field that might need dropping",
11083
"the previous content of the field will be dropped, which causes undefined \
11184
behavior if the field was not properly initialized",
11285
),
11386
AccessToUnionField => (
114-
Cow::Borrowed(self.simple_description()),
87+
"access to union field",
11588
"the field may not be properly initialized: using uninitialized data will cause \
11689
undefined behavior",
11790
),
11891
MutationOfLayoutConstrainedField => (
119-
Cow::Borrowed(self.simple_description()),
92+
"mutation of layout constrained field",
12093
"mutating layout constrained fields cannot statically be checked for valid values",
12194
),
12295
BorrowOfLayoutConstrainedField => (
123-
Cow::Borrowed(self.simple_description()),
96+
"borrow of layout constrained field with interior mutability",
12497
"references to fields of layout constrained fields lose the constraints. Coupled \
12598
with interior mutability, the field can be changed to invalid values",
12699
),
127-
CallToFunctionWith(did) => (
128-
Cow::from(format!(
129-
"call to function `{}` with `#[target_feature]`",
130-
tcx.def_path_str(*did)
131-
)),
100+
CallToFunctionWith => (
101+
"call to function with `#[target_feature]`",
132102
"can only be called if the required target features are available",
133103
),
134104
}

compiler/rustc_mir_transform/src/check_unsafety.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl<'tcx> Visitor<'tcx> for UnsafetyChecker<'_, 'tcx> {
7676
if let hir::Unsafety::Unsafe = sig.unsafety() {
7777
self.require_unsafe(
7878
UnsafetyViolationKind::General,
79-
UnsafetyViolationDetails::CallToUnsafeFunction(func_id.copied()),
79+
UnsafetyViolationDetails::CallToUnsafeFunction,
8080
)
8181
}
8282

@@ -381,7 +381,7 @@ impl<'tcx> UnsafetyChecker<'_, 'tcx> {
381381
if !callee_features.iter().all(|feature| self_features.contains(feature)) {
382382
self.require_unsafe(
383383
UnsafetyViolationKind::General,
384-
UnsafetyViolationDetails::CallToFunctionWith(func_did),
384+
UnsafetyViolationDetails::CallToFunctionWith,
385385
)
386386
}
387387
}
@@ -580,8 +580,7 @@ pub fn check_unsafety(tcx: TyCtxt<'_>, def_id: LocalDefId) {
580580
let UnsafetyCheckResult { violations, unused_unsafes, .. } = tcx.unsafety_check_result(def_id);
581581

582582
for &UnsafetyViolation { source_info, lint_root, kind, details } in violations.iter() {
583-
let (description, note) =
584-
ty::print::with_no_trimmed_paths!(details.description_and_note(tcx));
583+
let (description, note) = details.description_and_note();
585584

586585
// Report an error.
587586
let unsafe_fn_msg =
@@ -598,7 +597,7 @@ pub fn check_unsafety(tcx: TyCtxt<'_>, def_id: LocalDefId) {
598597
description,
599598
unsafe_fn_msg,
600599
)
601-
.span_label(source_info.span, details.simple_description())
600+
.span_label(source_info.span, description)
602601
.note(note)
603602
.emit();
604603
}

src/test/ui/async-await/async-unsafe-fn-call-in-safe.mir.stderr

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
error[E0133]: call to unsafe function `S::f` is unsafe and requires unsafe function or block
1+
error[E0133]: call to unsafe function is unsafe and requires unsafe function or block
22
--> $DIR/async-unsafe-fn-call-in-safe.rs:14:5
33
|
44
LL | S::f();
55
| ^^^^^^ call to unsafe function
66
|
77
= note: consult the function's documentation for information on how to avoid undefined behavior
88

9-
error[E0133]: call to unsafe function `f` is unsafe and requires unsafe function or block
10-
--> $DIR/async-unsafe-fn-call-in-safe.rs:15:5
9+
error[E0133]: call to unsafe function is unsafe and requires unsafe function or block
10+
--> $DIR/async-unsafe-fn-call-in-safe.rs:17:5
1111
|
1212
LL | f();
1313
| ^^^ call to unsafe function
1414
|
1515
= note: consult the function's documentation for information on how to avoid undefined behavior
1616

17-
error[E0133]: call to unsafe function `S::f` is unsafe and requires unsafe function or block
18-
--> $DIR/async-unsafe-fn-call-in-safe.rs:19:5
17+
error[E0133]: call to unsafe function is unsafe and requires unsafe function or block
18+
--> $DIR/async-unsafe-fn-call-in-safe.rs:23:5
1919
|
2020
LL | S::f();
2121
| ^^^^^^ call to unsafe function
2222
|
2323
= note: consult the function's documentation for information on how to avoid undefined behavior
2424

25-
error[E0133]: call to unsafe function `f` is unsafe and requires unsafe function or block
26-
--> $DIR/async-unsafe-fn-call-in-safe.rs:20:5
25+
error[E0133]: call to unsafe function is unsafe and requires unsafe function or block
26+
--> $DIR/async-unsafe-fn-call-in-safe.rs:24:5
2727
|
2828
LL | f();
2929
| ^^^ call to unsafe function

src/test/ui/async-await/async-unsafe-fn-call-in-safe.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@ impl S {
1111
async unsafe fn f() {}
1212

1313
async fn g() {
14-
S::f(); //~ ERROR call to unsafe function `S::f` is unsafe
15-
f(); //~ ERROR call to unsafe function `f` is unsafe
14+
S::f();
15+
//[mir]~^ ERROR call to unsafe function is unsafe
16+
//[thir]~^^ ERROR call to unsafe function `S::f` is unsafe
17+
f();
18+
//[mir]~^ ERROR call to unsafe function is unsafe
19+
//[thir]~^^ ERROR call to unsafe function `f` is unsafe
1620
}
1721

1822
fn main() {
19-
S::f(); //[mir]~ ERROR call to unsafe function `S::f` is unsafe
20-
f(); //[mir]~ ERROR call to unsafe function `f` is unsafe
23+
S::f(); //[mir]~ ERROR call to unsafe function is unsafe
24+
f(); //[mir]~ ERROR call to unsafe function is unsafe
2125
}

src/test/ui/async-await/async-unsafe-fn-call-in-safe.thir.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | S::f();
77
= note: consult the function's documentation for information on how to avoid undefined behavior
88

99
error[E0133]: call to unsafe function `f` is unsafe and requires unsafe function or block
10-
--> $DIR/async-unsafe-fn-call-in-safe.rs:15:5
10+
--> $DIR/async-unsafe-fn-call-in-safe.rs:17:5
1111
|
1212
LL | f();
1313
| ^^^ call to unsafe function

src/test/ui/closures/coerce-unsafe-closure-to-unsafe-fn-ptr.mir.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0133]: call to unsafe function `std::pin::Pin::<P>::new_unchecked` is unsafe and requires unsafe function or block
1+
error[E0133]: call to unsafe function is unsafe and requires unsafe function or block
22
--> $DIR/coerce-unsafe-closure-to-unsafe-fn-ptr.rs:5:31
33
|
44
LL | let _: unsafe fn() = || { ::std::pin::Pin::new_unchecked(&0_u8); };

src/test/ui/consts/const-extern-fn/const-extern-fn-requires-unsafe.mir.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
error[E0133]: call to unsafe function `foo` is unsafe and requires unsafe function or block
1+
error[E0133]: call to unsafe function is unsafe and requires unsafe function or block
22
--> $DIR/const-extern-fn-requires-unsafe.rs:9:17
33
|
44
LL | let a: [u8; foo()];
55
| ^^^^^ call to unsafe function
66
|
77
= note: consult the function's documentation for information on how to avoid undefined behavior
88

9-
error[E0133]: call to unsafe function `foo` is unsafe and requires unsafe function or block
10-
--> $DIR/const-extern-fn-requires-unsafe.rs:11:5
9+
error[E0133]: call to unsafe function is unsafe and requires unsafe function or block
10+
--> $DIR/const-extern-fn-requires-unsafe.rs:12:5
1111
|
1212
LL | foo();
1313
| ^^^^^ call to unsafe function

src/test/ui/consts/const-extern-fn/const-extern-fn-requires-unsafe.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ const unsafe extern "C" fn foo() -> usize { 5 }
77

88
fn main() {
99
let a: [u8; foo()];
10-
//~^ ERROR call to unsafe function `foo` is unsafe and requires unsafe function or block
10+
//[mir]~^ call to unsafe function is unsafe and requires unsafe function or block
11+
//[thir]~^^ call to unsafe function `foo` is unsafe and requires unsafe function or block
1112
foo();
12-
//[mir]~^ ERROR call to unsafe function `foo` is unsafe and requires unsafe function or block
13+
//[mir]~^ ERROR call to unsafe function is unsafe and requires unsafe function or block
1314
}

src/test/ui/error-codes/E0133.mir.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0133]: call to unsafe function `f` is unsafe and requires unsafe function or block
1+
error[E0133]: call to unsafe function is unsafe and requires unsafe function or block
22
--> $DIR/E0133.rs:7:5
33
|
44
LL | f();

src/test/ui/foreign-unsafe-fn-called.mir.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0133]: call to unsafe function `test::free` is unsafe and requires unsafe function or block
1+
error[E0133]: call to unsafe function is unsafe and requires unsafe function or block
22
--> $DIR/foreign-unsafe-fn-called.rs:11:5
33
|
44
LL | test::free();

src/test/ui/foreign-unsafe-fn-called.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ mod test {
99

1010
fn main() {
1111
test::free();
12-
//~^ ERROR call to unsafe function `test::free` is unsafe
12+
//[mir]~^ ERROR call to unsafe function is unsafe
13+
//[thir]~^^ ERROR call to unsafe function `test::free` is unsafe
1314
}

src/test/ui/intrinsics/unchecked_math_unsafe.mir.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
error[E0133]: call to unsafe function `std::intrinsics::unchecked_add` is unsafe and requires unsafe function or block
1+
error[E0133]: call to unsafe function is unsafe and requires unsafe function or block
22
--> $DIR/unchecked_math_unsafe.rs:8:15
33
|
44
LL | let add = std::intrinsics::unchecked_add(x, y);
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ call to unsafe function
66
|
77
= note: consult the function's documentation for information on how to avoid undefined behavior
88

9-
error[E0133]: call to unsafe function `std::intrinsics::unchecked_sub` is unsafe and requires unsafe function or block
9+
error[E0133]: call to unsafe function is unsafe and requires unsafe function or block
1010
--> $DIR/unchecked_math_unsafe.rs:9:15
1111
|
1212
LL | let sub = std::intrinsics::unchecked_sub(x, y);
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ call to unsafe function
1414
|
1515
= note: consult the function's documentation for information on how to avoid undefined behavior
1616

17-
error[E0133]: call to unsafe function `std::intrinsics::unchecked_mul` is unsafe and requires unsafe function or block
17+
error[E0133]: call to unsafe function is unsafe and requires unsafe function or block
1818
--> $DIR/unchecked_math_unsafe.rs:10:15
1919
|
2020
LL | let mul = std::intrinsics::unchecked_mul(x, y);

src/test/ui/issues/issue-28776.mir.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0133]: call to unsafe function `std::ptr::write` is unsafe and requires unsafe function or block
1+
error[E0133]: call to unsafe function is unsafe and requires unsafe function or block
22
--> $DIR/issue-28776.rs:7:5
33
|
44
LL | (&ptr::write)(1 as *mut _, 42);

src/test/ui/issues/issue-3080.mir.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0133]: call to unsafe function `X::with` is unsafe and requires unsafe function or block
1+
error[E0133]: call to unsafe function is unsafe and requires unsafe function or block
22
--> $DIR/issue-3080.rs:10:5
33
|
44
LL | X(()).with();

src/test/ui/issues/issue-5844.mir.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0133]: call to unsafe function `issue_5844_aux::rand` is unsafe and requires unsafe function or block
1+
error[E0133]: call to unsafe function is unsafe and requires unsafe function or block
22
--> $DIR/issue-5844.rs:8:5
33
|
44
LL | issue_5844_aux::rand();

0 commit comments

Comments
 (0)