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

Conversation

chenyukang
Copy link
Member

@chenyukang chenyukang commented Jul 13, 2024

Partially addresses #127562

@rustbot
Copy link
Collaborator

rustbot commented Jul 13, 2024

r? @fee1-dead

rustbot has assigned @fee1-dead.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 13, 2024
@fee1-dead
Copy link
Member

fee1-dead commented Jul 15, 2024

I have edited the PR description so that merging this PR does not close that issue. The best diagnostic improvement in that case would be suggesting addr_of! to be changed to addr_of_mut!.

@bors r+ rollup

@fee1-dead
Copy link
Member

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Jul 15, 2024

📌 Commit b44a484 has been approved by fee1-dead

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 15, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jul 15, 2024
… r=fee1-dead

Remove invalid help diagnostics for const pointer

Partially addresses rust-lang#127562
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 15, 2024
…iaskrgr

Rollup of 5 pull requests

Successful merges:

 - rust-lang#124921 (offset_from: always allow pointers to point to the same address)
 - rust-lang#127407 (Make parse error suggestions verbose and fix spans)
 - rust-lang#127675 (Remove invalid help diagnostics for const pointer)
 - rust-lang#127684 (consolidate miri-unleashed tests for mutable refs into one file)
 - rust-lang#127758 (coverage: Restrict `ExpressionUsed` simplification to `Code` mappings)

r? `@ghost`
`@rustbot` modify labels: rollup
@RalfJung
Copy link
Member

Wait but this still suggests ill-typed code, doesn't it?

@fee1-dead
Copy link
Member

Wait but this still suggests ill-typed code, doesn't it?

Huh. Hold on. The original issue doesn't even reproduce with latest nightly: https://rust.godbolt.org/z/c5vYcfY7d.

I'm less sure that this is actually fixing what it is supposed to be fixing.

@bors r-

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jul 15, 2024
@chenyukang
Copy link
Member Author

Wait but this still suggests ill-typed code, doesn't it?

Huh. Hold on. The original issue doesn't even reproduce with latest nightly: https://rust.godbolt.org/z/c5vYcfY7d.

I'm less sure that this is actually fixing what it is supposed to be fixing.

@bors r-

I think godbolt does some similar span remap with test UI framework.
We can reproduce the original issue on playground and nightly command line:
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=f0b69f8ac092cab9389293dffb523cba

I will have a dig on why the ill-type code suggestion comes out when span is remapped.

@RalfJung
Copy link
Member

It is also quite concerning that the ui test suite produces different results than playground... that's a disaster for testability of this bug. Is there an issue tracking that mismatch? Any idea how it can be fixed?

@chenyukang
Copy link
Member Author

chenyukang commented Jul 15, 2024

Seems the document for this is at here:
https://github.com/rust-lang/rustc-dev-guide/blob/d6e3a32a557db5902e714604def8015d6bb7e0f7/src/tests/ui.md#L114

The directory to the standard library source is replaced with $SRC_DIR. Example: /path/to/rust/library
Line and column numbers for paths in $SRC_DIR are replaced with LL:COL.
This helps ensure that changes to the layout of the standard library do not cause widespread changes to the .stderr files. Example: $SRC_DIR/alloc/src/sync.rs:53:46

so when the code logic is base on something like tcx.sess.source_map().span_to_snippet(span), it will return an Err:

Err(SourceNotAvailable { filename: Real(Remapped { local_path: None, virtual_name: "$SRC_DIR/core/src/ptr/mod.rs" }) })

then the code flow changes...

@chenyukang
Copy link
Member Author

chenyukang commented Jul 15, 2024

This new change will resolve this specific invalid suggestion in the UI test:
https://github.com/rust-lang/rust/pull/127675/files#diff-23edf23f7466b97eb3154b82f7b324983287c327937fd66e854b3df6759926ceR1149-R1154

@oli-obk told me we had several issues like this, I'm not sure whether we can get an more general to avoid different results between the UI test and playground.

@RalfJung
Copy link
Member

Seems the document for this is at here: https://github.com/rust-lang/rustc-dev-guide/blob/d6e3a32a557db5902e714604def8015d6bb7e0f7/src/tests/ui.md#L114

The directory to the standard library source is replaced with $SRC_DIR. Example: /path/to/rust/library
Line and column numbers for paths in $SRC_DIR are replaced with LL:COL.
This helps ensure that changes to the layout of the standard library do not cause widespread changes to the .stderr files. Example: $SRC_DIR/alloc/src/sync.rs:53:46

so when the code logic is base on something like tcx.sess.source_map().span_to_snippet(span), it will return an Err:

Err(SourceNotAvailable { filename: Real(Remapped { local_path: None, virtual_name: "$SRC_DIR/core/src/ptr/mod.rs" }) })

then the code flow changes...

No, that just does some post-processing of the output, so that can't be it.

@oli-obk do you understand what is going on here? We should definitely have an issue about this -- when I copy some code from the playground to a ui test, it should behave the same, otherwise we aren't even testing the thing that people will be using in the wild!

@oli-obk
Copy link
Contributor

oli-obk commented Jul 16, 2024

idk, we do try to actually do what happens outside of the test suite, but there are some corner cases around libstd that are not easy and no one felt like it's too important to actually work on it.

@lolbinarycat lolbinarycat added the A-diagnostics Area: Messages for errors, warnings, and lints label Sep 1, 2024
@alex-semenyuk alex-semenyuk added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 11, 2024
@alex-semenyuk
Copy link
Member

@chenyukang
From wg-triage. Could you please clarify the status of PR?

@chenyukang
Copy link
Member Author

I think we could merge this PR first and create a new issue to track the test suite issue in #127675 (comment) ?

@chenyukang
Copy link
Member Author

cc @fee1-dead

@alex-semenyuk alex-semenyuk added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 14, 2024
@fee1-dead
Copy link
Member

r? compiler

@rustbot rustbot assigned petrochenkov and unassigned fee1-dead Oct 16, 2024
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?

@RalfJung
Copy link
Member

It is also quite concerning that the ui test suite produces different results than playground... that's a disaster for testability of this bug. Is there an issue tracking that mismatch? Any idea how it can be fixed?

I filed an issue for that: #131782

@petrochenkov
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Oct 19, 2024

📌 Commit 7ff71e5 has been approved by petrochenkov

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 19, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 19, 2024
…iaskrgr

Rollup of 12 pull requests

Successful merges:

 - rust-lang#116863 (warn less about non-exhaustive in ffi)
 - rust-lang#127675 (Remove invalid help diagnostics for const pointer)
 - rust-lang#131772 (Remove `const_refs_to_static` TODO in proc_macro)
 - rust-lang#131789 (Make sure that outer opaques capture inner opaques's lifetimes even with precise capturing syntax)
 - rust-lang#131795 (Stop inverting expectation in normalization errors)
 - rust-lang#131920 (Add codegen test for branchy bool match)
 - rust-lang#131921 (replace STATX_ALL with (STATX_BASIC_STATS | STATX_BTIME) as former is deprecated)
 - rust-lang#131925 (Warn on redundant `--cfg` directive when revisions are used)
 - rust-lang#131931 (Remove unnecessary constness from `lower_generic_args_of_path`)
 - rust-lang#131932 (use tracked_path in rustc_fluent_macro)
 - rust-lang#131936 (feat(rustdoc-json-types): introduce rustc-hash feature)
 - rust-lang#131939 (Get rid of `OnlySelfBounds`)

Failed merges:

 - rust-lang#131181 (Compiletest: Custom differ)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 268fa31 into rust-lang:master Oct 20, 2024
6 checks passed
@rustbot rustbot added this to the 1.84.0 milestone Oct 20, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Oct 20, 2024
Rollup merge of rust-lang#127675 - chenyukang:yukang-fix-127562-addr, r=petrochenkov

Remove invalid help diagnostics for const pointer

Partially addresses rust-lang#127562
@cyrgani
Copy link
Contributor

cyrgani commented Oct 21, 2024

This PR does not even fully solve the invalid diagnostic problem though: while addr_of! no longer suggests &mut raw const val, expanding the macro (which is recommended) does:

fn main() {
    let val = 2;
    let ptr = &raw const val;
    unsafe { *ptr = 3; }
}

Error:

error[E0594]: cannot assign to `*ptr`, which is behind a `*const` pointer
 --> src/main.rs:4:14
  |
4 |     unsafe { *ptr = 3; }
  |              ^^^^^^^^ `ptr` is a `*const` pointer, so the data it refers to cannot be written
  |
help: consider changing this to be a mutable pointer
  |
3 |     let ptr = &mut raw const val;
  |                +++

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

@chenyukang
Copy link
Member Author

This PR does not even fully solve the invalid diagnostic problem though: while addr_of! no longer suggests &mut raw const val, expanding the macro (which is recommended) does:

fn main() {
    let val = 2;
    let ptr = &raw const val;
    unsafe { *ptr = 3; }
}

Error:

error[E0594]: cannot assign to `*ptr`, which is behind a `*const` pointer
 --> src/main.rs:4:14
  |
4 |     unsafe { *ptr = 3; }
  |              ^^^^^^^^ `ptr` is a `*const` pointer, so the data it refers to cannot be written
  |
help: consider changing this to be a mutable pointer
  |
3 |     let ptr = &mut raw const val;
  |                +++

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

Yes, the original issue is not closed, this PR only remove this part, we should not suggest code change for any span which is not written by user:

help: consider changing this to be a mutable pointer
 --> /home/user/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ptr/mod.rs:2189:6
  |
21|     &mut raw const $place
  |      +++

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.