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

-Copt-level=2 may miscompile valid code #131761

Closed
shao-hua-li opened this issue Oct 15, 2024 · 1 comment
Closed

-Copt-level=2 may miscompile valid code #131761

shao-hua-li opened this issue Oct 15, 2024 · 1 comment
Labels
C-discussion Category: Discussion or questions that doesn't represent real issues.

Comments

@shao-hua-li
Copy link

I tried this code:

pub fn a() -> i32 {
    return 0;
}
pub fn b() -> i32 {
    return 0;
}
pub fn main() {
    if Some(b as fn() -> i32) != Some(a as fn() -> i32) {
        println!("OK\n");
    }
}

I expected to see this happen: -Copt-level=0/1/2/3 should give the same result

Instead, this happened:

% rustc test.rs -Copt-level=1 &&./test
OK
% rustc test.rs -Copt-level=2 &&./test
%

Meta

rustc --version --verbose:

rustc 1.84.0-nightly (9322d183f 2024-10-14)
binary: rustc
commit-hash: 9322d183f45e0fd5a509820874cc5ff27744a479
commit-date: 2024-10-14
host: x86_64-unknown-linux-gnu
release: 1.84.0-nightly
LLVM version: 19.1.1
@shao-hua-li shao-hua-li added the C-bug Category: This is a bug. label Oct 15, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 15, 2024
@saethlin saethlin added C-discussion Category: Discussion or questions that doesn't represent real issues. and removed C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Oct 15, 2024
@saethlin
Copy link
Member

saethlin commented Oct 15, 2024

This is not a bug, you're tripping over rust-lang/unsafe-code-guidelines#522.

In short:

Functions, vtables, and consts do not have a unique stable address

The program above has different behavior because the functions are merged at some opt levels. Note that function addresses truly are unstable, not only can you get merges like this, a single function can have multiple addresses in a single program.

@Noratrieb Noratrieb closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-discussion Category: Discussion or questions that doesn't represent real issues.
Projects
None yet
Development

No branches or pull requests

4 participants