Skip to content

Commit b44a484

Browse files
committed
Remove invalid help diagnostics for const pointer
1 parent 03c2100 commit b44a484

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+15-12
Original file line numberDiff line numberDiff line change
@@ -1198,18 +1198,21 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
11981198
sugg.push(s);
11991199
}
12001200

1201-
err.multipart_suggestion_verbose(
1202-
format!(
1203-
"consider changing this to be a mutable {pointer_desc}{}",
1204-
if is_trait_sig {
1205-
" in the `impl` method and the `trait` definition"
1206-
} else {
1207-
""
1208-
}
1209-
),
1210-
sugg,
1211-
Applicability::MachineApplicable,
1212-
);
1201+
if sugg.iter().all(|(span, _)| !self.infcx.tcx.sess.source_map().is_imported(*span))
1202+
{
1203+
err.multipart_suggestion_verbose(
1204+
format!(
1205+
"consider changing this to be a mutable {pointer_desc}{}",
1206+
if is_trait_sig {
1207+
" in the `impl` method and the `trait` definition"
1208+
} else {
1209+
""
1210+
}
1211+
),
1212+
sugg,
1213+
Applicability::MachineApplicable,
1214+
);
1215+
}
12131216
}
12141217
Some((false, err_label_span, message, _)) => {
12151218
let def_id = self.body.source.def_id();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn main() {
2+
let val = 2;
3+
let ptr = std::ptr::addr_of!(val);
4+
unsafe {
5+
*ptr = 3; //~ ERROR cannot assign to `*ptr`, which is behind a `*const` pointer
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0594]: cannot assign to `*ptr`, which is behind a `*const` pointer
2+
--> $DIR/dont_suggest_raw_pointer_syntax-issue-127562.rs:5:9
3+
|
4+
LL | *ptr = 3;
5+
| ^^^^^^^^ `ptr` is a `*const` pointer, so the data it refers to cannot be written
6+
|
7+
help: consider specifying this binding's type
8+
|
9+
LL | let ptr: *mut i32 = std::ptr::addr_of!(val);
10+
| ++++++++++
11+
12+
error: aborting due to 1 previous error
13+
14+
For more information about this error, try `rustc --explain E0594`.

0 commit comments

Comments
 (0)