Skip to content

Type inference not working well with ptr mutability #124665

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

Open
terrarier2111 opened this issue May 3, 2024 · 2 comments
Open

Type inference not working well with ptr mutability #124665

terrarier2111 opened this issue May 3, 2024 · 2 comments
Labels
A-inference Area: Type inference C-bug Category: This is a bug. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@terrarier2111
Copy link
Contributor

terrarier2111 commented May 3, 2024

I tried this code:

fn main() {
    let mut tmp: usize = 3;
    let immut_addr = &tmp as *const _;
    let addr = immut_addr.cast_mut();
}

I expected to see this happen: the code compiles successfully (but with unused warnings)

Instead, this happened: The compiler fails to infer the type of addr because it doesn't recognize that the type it inferred for immut_addr is always the same as the one cast_mut produces

On stable the compiler suggests to add : *const _ to immut_addr as a type annotation, but after adding this, it errors the same way it did before.

Meta

rustc --version --verbose:
This happens both on stable (1.78) and on the latest nightly

rustc 1.80.0-nightly (1fffb2a35 2024-04-28)
binary: rustc
commit-hash: 1fffb2a355dfee65bf6aa5a9db35f33d3ee93aec
commit-date: 2024-04-28
host: x86_64-unknown-linux-gnu
release: 1.80.0-nightly
LLVM version: 18.1.4
Output

error[E0699]: cannot call a method on a raw pointer with an unknown pointee type
 --> src/main.rs:6:27
  |
6 |     let addr = immut_addr.cast_mut();
  | 

@terrarier2111 terrarier2111 added the C-bug Category: This is a bug. label May 3, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 3, 2024
@veera-sivarajan
Copy link
Contributor

Looks like this behavior is documented in https://doc.rust-lang.org/error_codes/E0699.html?

As an aside, the current nightly produces a completely different error for the example. Don't know why

@veera-sivarajan
Copy link
Contributor

Nevermind, the current nightly's different error is intentional: #122768

@jieyouxu jieyouxu added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-inference Area: Type inference S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-inference Area: Type inference C-bug Category: This is a bug. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants