Skip to content

Commit 2e47271

Browse files
committed
only show a simple description in E0133 span label
1 parent 8b8f665 commit 2e47271

33 files changed

+126
-87
lines changed

compiler/rustc_middle/src/mir/query.rs

+32-11
Original file line numberDiff line numberDiff line change
@@ -44,62 +44,83 @@ pub enum UnsafetyViolationDetails {
4444
}
4545

4646
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+
4768
pub fn description_and_note(&self, tcx: TyCtxt<'_>) -> (Cow<'static, str>, &'static str) {
4869
use UnsafetyViolationDetails::*;
4970
match self {
5071
CallToUnsafeFunction(did) => (
5172
if let Some(did) = did {
5273
Cow::from(format!("call to unsafe function `{}`", tcx.def_path_str(*did)))
5374
} else {
54-
Cow::Borrowed("call to unsafe function")
75+
Cow::Borrowed(self.simple_description())
5576
},
5677
"consult the function's documentation for information on how to avoid undefined \
5778
behavior",
5879
),
5980
UseOfInlineAssembly => (
60-
Cow::Borrowed("use of inline assembly"),
81+
Cow::Borrowed(self.simple_description()),
6182
"inline assembly is entirely unchecked and can cause undefined behavior",
6283
),
6384
InitializingTypeWith => (
64-
Cow::Borrowed("initializing type with `rustc_layout_scalar_valid_range` attr"),
85+
Cow::Borrowed(self.simple_description()),
6586
"initializing a layout restricted type's field with a value outside the valid \
6687
range is undefined behavior",
6788
),
6889
CastOfPointerToInt => (
69-
Cow::Borrowed("cast of pointer to int"),
90+
Cow::Borrowed(self.simple_description()),
7091
"casting pointers to integers in constants",
7192
),
7293
UseOfMutableStatic => (
73-
Cow::Borrowed("use of mutable static"),
94+
Cow::Borrowed(self.simple_description()),
7495
"mutable statics can be mutated by multiple threads: aliasing violations or data \
7596
races will cause undefined behavior",
7697
),
7798
UseOfExternStatic => (
78-
Cow::Borrowed("use of extern static"),
99+
Cow::Borrowed(self.simple_description()),
79100
"extern statics are not controlled by the Rust type system: invalid data, \
80101
aliasing violations or data races will cause undefined behavior",
81102
),
82103
DerefOfRawPointer => (
83-
Cow::Borrowed("dereference of raw pointer"),
104+
Cow::Borrowed(self.simple_description()),
84105
"raw pointers may be null, dangling or unaligned; they can violate aliasing rules \
85106
and cause data races: all of these are undefined behavior",
86107
),
87108
AssignToDroppingUnionField => (
88-
Cow::Borrowed("assignment to union field that might need dropping"),
109+
Cow::Borrowed(self.simple_description()),
89110
"the previous content of the field will be dropped, which causes undefined \
90111
behavior if the field was not properly initialized",
91112
),
92113
AccessToUnionField => (
93-
Cow::Borrowed("access to union field"),
114+
Cow::Borrowed(self.simple_description()),
94115
"the field may not be properly initialized: using uninitialized data will cause \
95116
undefined behavior",
96117
),
97118
MutationOfLayoutConstrainedField => (
98-
Cow::Borrowed("mutation of layout constrained field"),
119+
Cow::Borrowed(self.simple_description()),
99120
"mutating layout constrained fields cannot statically be checked for valid values",
100121
),
101122
BorrowOfLayoutConstrainedField => (
102-
Cow::Borrowed("borrow of layout constrained field with interior mutability"),
123+
Cow::Borrowed(self.simple_description()),
103124
"references to fields of layout constrained fields lose the constraints. Coupled \
104125
with interior mutability, the field can be changed to invalid values",
105126
),

compiler/rustc_mir_build/src/check_unsafety.rs

+30-12
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
9393
"{} is unsafe and requires unsafe block (error E0133)",
9494
description,
9595
))
96-
.span_label(span, description)
96+
.span_label(span, kind.simple_description())
9797
.note(note)
9898
.emit();
9999
},
@@ -110,7 +110,7 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
110110
description,
111111
fn_sugg,
112112
)
113-
.span_label(span, description)
113+
.span_label(span, kind.simple_description())
114114
.note(note)
115115
.emit();
116116
}
@@ -546,57 +546,75 @@ enum UnsafeOpKind {
546546
use UnsafeOpKind::*;
547547

548548
impl UnsafeOpKind {
549+
pub fn simple_description(&self) -> &'static str {
550+
match self {
551+
CallToUnsafeFunction(..) => "call to unsafe function",
552+
UseOfInlineAssembly => "use of inline assembly",
553+
InitializingTypeWith => "initializing type with `rustc_layout_scalar_valid_range` attr",
554+
UseOfMutableStatic => "use of mutable static",
555+
UseOfExternStatic => "use of extern static",
556+
DerefOfRawPointer => "dereference of raw pointer",
557+
AssignToDroppingUnionField => "assignment to union field that might need dropping",
558+
AccessToUnionField => "access to union field",
559+
MutationOfLayoutConstrainedField => "mutation of layout constrained field",
560+
BorrowOfLayoutConstrainedField => {
561+
"borrow of layout constrained field with interior mutability"
562+
}
563+
CallToFunctionWith(..) => "call to function with `#[target_feature]`",
564+
}
565+
}
566+
549567
pub fn description_and_note(&self, tcx: TyCtxt<'_>) -> (Cow<'static, str>, &'static str) {
550568
match self {
551569
CallToUnsafeFunction(did) => (
552570
if let Some(did) = did {
553571
Cow::from(format!("call to unsafe function `{}`", tcx.def_path_str(*did)))
554572
} else {
555-
Cow::Borrowed("call to unsafe function")
573+
Cow::Borrowed(self.simple_description())
556574
},
557575
"consult the function's documentation for information on how to avoid undefined \
558576
behavior",
559577
),
560578
UseOfInlineAssembly => (
561-
Cow::Borrowed("use of inline assembly"),
579+
Cow::Borrowed(self.simple_description()),
562580
"inline assembly is entirely unchecked and can cause undefined behavior",
563581
),
564582
InitializingTypeWith => (
565-
Cow::Borrowed("initializing type with `rustc_layout_scalar_valid_range` attr"),
583+
Cow::Borrowed(self.simple_description()),
566584
"initializing a layout restricted type's field with a value outside the valid \
567585
range is undefined behavior",
568586
),
569587
UseOfMutableStatic => (
570-
Cow::Borrowed("use of mutable static"),
588+
Cow::Borrowed(self.simple_description()),
571589
"mutable statics can be mutated by multiple threads: aliasing violations or data \
572590
races will cause undefined behavior",
573591
),
574592
UseOfExternStatic => (
575-
Cow::Borrowed("use of extern static"),
593+
Cow::Borrowed(self.simple_description()),
576594
"extern statics are not controlled by the Rust type system: invalid data, \
577595
aliasing violations or data races will cause undefined behavior",
578596
),
579597
DerefOfRawPointer => (
580-
Cow::Borrowed("dereference of raw pointer"),
598+
Cow::Borrowed(self.simple_description()),
581599
"raw pointers may be null, dangling or unaligned; they can violate aliasing rules \
582600
and cause data races: all of these are undefined behavior",
583601
),
584602
AssignToDroppingUnionField => (
585-
Cow::Borrowed("assignment to union field that might need dropping"),
603+
Cow::Borrowed(self.simple_description()),
586604
"the previous content of the field will be dropped, which causes undefined \
587605
behavior if the field was not properly initialized",
588606
),
589607
AccessToUnionField => (
590-
Cow::Borrowed("access to union field"),
608+
Cow::Borrowed(self.simple_description()),
591609
"the field may not be properly initialized: using uninitialized data will cause \
592610
undefined behavior",
593611
),
594612
MutationOfLayoutConstrainedField => (
595-
Cow::Borrowed("mutation of layout constrained field"),
613+
Cow::Borrowed(self.simple_description()),
596614
"mutating layout constrained fields cannot statically be checked for valid values",
597615
),
598616
BorrowOfLayoutConstrainedField => (
599-
Cow::Borrowed("borrow of layout constrained field with interior mutability"),
617+
Cow::Borrowed(self.simple_description()),
600618
"references to fields of layout constrained fields lose the constraints. Coupled \
601619
with interior mutability, the field can be changed to invalid values",
602620
),

compiler/rustc_mir_transform/src/check_unsafety.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ pub fn check_unsafety(tcx: TyCtxt<'_>, def_id: LocalDefId) {
598598
description,
599599
unsafe_fn_msg,
600600
)
601-
.span_label(source_info.span, description)
601+
.span_label(source_info.span, details.simple_description())
602602
.note(note)
603603
.emit();
604604
}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,31 @@ error[E0133]: call to unsafe function `S::f` is unsafe and requires unsafe funct
22
--> $DIR/async-unsafe-fn-call-in-safe.rs:14:5
33
|
44
LL | S::f();
5-
| ^^^^^^ call to unsafe function `S::f`
5+
| ^^^^^^ call to unsafe function
66
|
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
1010
--> $DIR/async-unsafe-fn-call-in-safe.rs:15:5
1111
|
1212
LL | f();
13-
| ^^^ call to unsafe function `f`
13+
| ^^^ call to unsafe function
1414
|
1515
= note: consult the function's documentation for information on how to avoid undefined behavior
1616

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

2525
error[E0133]: call to unsafe function `f` is unsafe and requires unsafe function or block
2626
--> $DIR/async-unsafe-fn-call-in-safe.rs:20:5
2727
|
2828
LL | f();
29-
| ^^^ call to unsafe function `f`
29+
| ^^^ call to unsafe function
3030
|
3131
= note: consult the function's documentation for information on how to avoid undefined behavior
3232

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ error[E0133]: call to unsafe function `S::f` is unsafe and requires unsafe funct
22
--> $DIR/async-unsafe-fn-call-in-safe.rs:14:5
33
|
44
LL | S::f();
5-
| ^^^^^^ call to unsafe function `S::f`
5+
| ^^^^^^ call to unsafe function
66
|
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
1010
--> $DIR/async-unsafe-fn-call-in-safe.rs:15:5
1111
|
1212
LL | f();
13-
| ^^^ call to unsafe function `f`
13+
| ^^^ call to unsafe function
1414
|
1515
= note: consult the function's documentation for information on how to avoid undefined behavior
1616

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0133]: call to unsafe function `std::pin::Pin::<P>::new_unchecked` is uns
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); };
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ call to unsafe function `std::pin::Pin::<P>::new_unchecked`
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ call to unsafe function
66
|
77
= note: consult the function's documentation for information on how to avoid undefined behavior
88

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0133]: call to unsafe function `Pin::<P>::new_unchecked` is unsafe and re
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); };
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ call to unsafe function `Pin::<P>::new_unchecked`
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ call to unsafe function
66
|
77
= note: consult the function's documentation for information on how to avoid undefined behavior
88

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

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

99
error[E0133]: call to unsafe function `foo` is unsafe and requires unsafe function or block
1010
--> $DIR/const-extern-fn-requires-unsafe.rs:11:5
1111
|
1212
LL | foo();
13-
| ^^^^^ call to unsafe function `foo`
13+
| ^^^^^ call to unsafe function
1414
|
1515
= note: consult the function's documentation for information on how to avoid undefined behavior
1616

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

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

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

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

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

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

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0133]: call to unsafe function `test::free` is unsafe and requires unsafe
22
--> $DIR/foreign-unsafe-fn-called.rs:11:5
33
|
44
LL | test::free();
5-
| ^^^^^^^^^^^^ call to unsafe function `test::free`
5+
| ^^^^^^^^^^^^ call to unsafe function
66
|
77
= note: consult the function's documentation for information on how to avoid undefined behavior
88

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0133]: call to unsafe function `test::free` is unsafe and requires unsafe
22
--> $DIR/foreign-unsafe-fn-called.rs:11:5
33
|
44
LL | test::free();
5-
| ^^^^^^^^^^^^ call to unsafe function `test::free`
5+
| ^^^^^^^^^^^^ call to unsafe function
66
|
77
= note: consult the function's documentation for information on how to avoid undefined behavior
88

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

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

99
error[E0133]: call to unsafe function `std::intrinsics::unchecked_sub` 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);
13-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ call to unsafe function `std::intrinsics::unchecked_sub`
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ call to unsafe function
1414
|
1515
= note: consult the function's documentation for information on how to avoid undefined behavior
1616

1717
error[E0133]: call to unsafe function `std::intrinsics::unchecked_mul` 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);
21-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ call to unsafe function `std::intrinsics::unchecked_mul`
21+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ call to unsafe function
2222
|
2323
= note: consult the function's documentation for information on how to avoid undefined behavior
2424

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

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

99
error[E0133]: call to unsafe function `unchecked_sub` 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);
13-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ call to unsafe function `unchecked_sub`
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ call to unsafe function
1414
|
1515
= note: consult the function's documentation for information on how to avoid undefined behavior
1616

1717
error[E0133]: call to unsafe function `unchecked_mul` 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);
21-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ call to unsafe function `unchecked_mul`
21+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ call to unsafe function
2222
|
2323
= note: consult the function's documentation for information on how to avoid undefined behavior
2424

0 commit comments

Comments
 (0)