Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove invalid help diagnostics for const pointer #127675

Merged
merged 1 commit into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1146,6 +1146,12 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
}
// don't create labels for compiler-generated spans
Some(_) => None,
// don't create labels for the span not from user's code
None if opt_assignment_rhs_span
.is_some_and(|span| self.infcx.tcx.sess.source_map().is_imported(span)) =>
{
None
}
None => {
let (has_sugg, decl_span, sugg) = if name != kw::SelfLower {
suggest_ampmut(
Expand Down Expand Up @@ -1198,18 +1204,21 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
sugg.push(s);
}

err.multipart_suggestion_verbose(
format!(
"consider changing this to be a mutable {pointer_desc}{}",
if is_trait_sig {
" in the `impl` method and the `trait` definition"
} else {
""
}
),
sugg,
Applicability::MachineApplicable,
);
if sugg.iter().all(|(span, _)| !self.infcx.tcx.sess.source_map().is_imported(*span))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The source of the problem here is that the suggestion points inside a macro. This is IMO not something that should be fixed in this specific diagnostic, it affects all daignostics and thus needs a central fix.

I don't know the diagnostic system well enough though to suggest how this could be fixed.
Cc @rust-lang/wg-diagnostics

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, that's not what this code does though?

{
err.multipart_suggestion_verbose(
format!(
"consider changing this to be a mutable {pointer_desc}{}",
if is_trait_sig {
" in the `impl` method and the `trait` definition"
} else {
""
}
),
sugg,
Applicability::MachineApplicable,
);
}
}
Some((false, err_label_span, message, _)) => {
let def_id = self.body.source.def_id();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn main() {
let val = 2;
let ptr = std::ptr::addr_of!(val);
unsafe {
*ptr = 3; //~ ERROR cannot assign to `*ptr`, which is behind a `*const` pointer
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0594]: cannot assign to `*ptr`, which is behind a `*const` pointer
--> $DIR/dont_suggest_raw_pointer_syntax-issue-127562.rs:5:9
|
LL | *ptr = 3;
| ^^^^^^^^ `ptr` is a `*const` pointer, so the data it refers to cannot be written

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0594`.
Loading