forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#127003 - GrigorenkoPV:107975, r=SparrowLii
Add a test for rust-lang#107975 The int is zero. But also not zero. This is so much fun. This is a part of rust-lang#105107. Initially I was going to just rebase rust-lang#108445, but quite a few things changed since then: * The [mcve](rust-lang#105787 (comment)) used for rust-lang#105787 got fixed.[^upd2] * You can't just `a ?= b` for rust-lang#107975 anymore. Now you have to `a-b ?= 0`. This is what this PR does. As an additional flex, it show that three ways of converting a pointer to its address have this issue: 1. `as usize` 2. `.expose_provenance()` 3. `.addr()` * rust-lang#108425 simply got fixed. Yay. As an aside, the naming for `addr_of!` is quite unfortunate in context of provenance APIs. Because `addr_of!` gives you a pointer, but what provenance APIs refer to as "address" is the `usize` value. Oh well. UPD1: GitHub is incapable of parsing rust-lang#107975 in the PR name, so let's add it here. [^upd2]: UPD2: [The other mcve](rust-lang#105787 (comment)) does not work anymore either, saying "this behavior recently changed as a result of a bug fix; see rust-lang#56105 for details."
- Loading branch information
Showing
37 changed files
with
753 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
See https://github.com/rust-lang/rust/issues/107975 | ||
|
||
Basically, if you have two pointers with the same address but from two different allocations, | ||
the compiler gets confused whether their addresses are equal or not, | ||
resulting in some self-contradictory behavior of the compiled code. | ||
|
||
This folder contains some examples. | ||
They all boil down to allocating a variable on the stack, taking its address, | ||
getting rid of the variable, and then doing it all again. | ||
This way we end up with two addresses stored in two `usize`s (`a` and `b`). | ||
The addresses are (probably) equal but (definitely) come from two different allocations. | ||
Logically, we would expect that exactly one of the following options holds true: | ||
1. `a == b` | ||
2. `a != b` | ||
Sadly, the compiler does not always agree. | ||
|
||
Due to Rust having at least three meaningfully different ways | ||
to get a variable's address as an `usize`, | ||
each example is provided in three versions, each in the corresponding subfolder: | ||
1. `./as-cast/` for `&v as *const _ as usize`, | ||
2. `./strict-provenance/` for `addr_of!(v).addr()`, | ||
2. `./exposed-provenance/` for `addr_of!(v).expose_provenance()`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
//@ known-bug: #107975 | ||
//@ compile-flags: -Copt-level=2 | ||
//@ run-pass | ||
|
||
fn main() { | ||
let a: usize = { | ||
let v = 0u8; | ||
&v as *const _ as usize | ||
}; | ||
let b: usize = { | ||
let v = 0u8; | ||
&v as *const _ as usize | ||
}; | ||
|
||
// `a` and `b` are not equal. | ||
assert_ne!(a, b); | ||
// But they are the same number. | ||
assert_eq!(format!("{a}"), format!("{b}")); | ||
// And they are equal. | ||
assert_eq!(a, b); | ||
} |
22 changes: 22 additions & 0 deletions
22
tests/ui/codegen/equal-pointers-unequal/as-cast/function.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
//@ known-bug: #107975 | ||
//@ compile-flags: -Copt-level=2 | ||
//@ run-pass | ||
|
||
// Based on https://github.com/rust-lang/rust/issues/107975#issuecomment-1434203908 | ||
|
||
fn f() -> usize { | ||
let v = 0; | ||
&v as *const _ as usize | ||
} | ||
|
||
fn main() { | ||
let a = f(); | ||
let b = f(); | ||
|
||
// `a` and `b` are not equal. | ||
assert_ne!(a, b); | ||
// But they are the same number. | ||
assert_eq!(format!("{a}"), format!("{b}")); | ||
// And they are equal. | ||
assert_eq!(a, b); | ||
} |
30 changes: 30 additions & 0 deletions
30
tests/ui/codegen/equal-pointers-unequal/as-cast/inline1.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
//@ known-bug: #107975 | ||
//@ compile-flags: -Copt-level=2 | ||
//@ run-pass | ||
//@ check-run-results | ||
//@ normalize-stdout-test: "\d+" -> "<..>" | ||
|
||
// Based on https://github.com/rust-lang/rust/issues/107975#issuecomment-1432161340 | ||
|
||
#[inline(never)] | ||
fn cmp(a: usize, b: usize) -> bool { | ||
a == b | ||
} | ||
|
||
#[inline(always)] | ||
fn cmp_in(a: usize, b: usize) -> bool { | ||
a == b | ||
} | ||
|
||
fn main() { | ||
let a = { | ||
let v = 0; | ||
&v as *const _ as usize | ||
}; | ||
let b = { | ||
let v = 0; | ||
&v as *const _ as usize | ||
}; | ||
println!("{a:?} == {b:?} -> ==: {}, cmp_in: {}, cmp: {}", a == b, cmp_in(a, b), cmp(a, b)); | ||
assert_eq!(a.to_string(), b.to_string()); | ||
} |
1 change: 1 addition & 0 deletions
1
tests/ui/codegen/equal-pointers-unequal/as-cast/inline1.run.stdout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<..> == <..> -> ==: false, cmp_in: false, cmp: true |
30 changes: 30 additions & 0 deletions
30
tests/ui/codegen/equal-pointers-unequal/as-cast/inline2.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
//@ known-bug: #107975 | ||
//@ compile-flags: -Copt-level=2 | ||
//@ run-pass | ||
//@ check-run-results | ||
//@ normalize-stdout-test: "\d+" -> "<..>" | ||
|
||
// Based on https://github.com/rust-lang/rust/issues/107975#issuecomment-1432161340 | ||
|
||
#[inline(never)] | ||
fn cmp(a: usize, b: usize) -> bool { | ||
a == b | ||
} | ||
|
||
#[inline(always)] | ||
fn cmp_in(a: usize, b: usize) -> bool { | ||
a == b | ||
} | ||
|
||
fn main() { | ||
let a = { | ||
let v = 0; | ||
&v as *const _ as usize | ||
}; | ||
let b = { | ||
let v = 0; | ||
&v as *const _ as usize | ||
}; | ||
assert_eq!(a.to_string(), b.to_string()); | ||
println!("{a:?} == {b:?} -> ==: {}, cmp_in: {}, cmp: {}", a == b, cmp_in(a, b), cmp(a, b)); | ||
} |
1 change: 1 addition & 0 deletions
1
tests/ui/codegen/equal-pointers-unequal/as-cast/inline2.run.stdout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<..> == <..> -> ==: true, cmp_in: true, cmp: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
//@ known-bug: #107975 | ||
//@ compile-flags: -Copt-level=2 | ||
//@ run-pass | ||
//@ check-run-results | ||
//@ normalize-stdout-test: "\d+" -> "<..>" | ||
|
||
// https://github.com/rust-lang/rust/issues/107975#issuecomment-1430704499 | ||
|
||
fn main() { | ||
let a = { | ||
let v = 0; | ||
&v as *const _ as usize | ||
}; | ||
let b = { | ||
let v = 0; | ||
&v as *const _ as usize | ||
}; | ||
|
||
println!("{}", a == b); // prints false | ||
println!("{a}"); // or b | ||
println!("{}", a == b); // prints true | ||
} |
3 changes: 3 additions & 0 deletions
3
tests/ui/codegen/equal-pointers-unequal/as-cast/print.run.stdout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
false | ||
<..> | ||
true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//@ known-bug: #107975 | ||
//@ compile-flags: -Copt-level=2 | ||
//@ run-pass | ||
//@ check-run-results | ||
//@ normalize-stdout-test: "\d+" -> "<..>" | ||
|
||
// https://github.com/rust-lang/rust/issues/107975#issuecomment-1430704499 | ||
|
||
fn main() { | ||
let a = { | ||
let v = 0; | ||
&v as *const _ as usize | ||
}; | ||
let b = { | ||
let v = 0; | ||
&v as *const _ as usize | ||
}; | ||
|
||
println!("{}", a == b); // false | ||
println!("{}", a == b); // false | ||
let c = a; | ||
println!("{} {} {}", a == b, a == c, b == c); // false true false | ||
println!("{a} {b}"); | ||
println!("{} {} {}", a == b, a == c, b == c); // true true true | ||
} |
5 changes: 5 additions & 0 deletions
5
tests/ui/codegen/equal-pointers-unequal/as-cast/print3.run.stdout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
false | ||
false | ||
false true false | ||
<..> <..> | ||
true true true |
35 changes: 35 additions & 0 deletions
35
tests/ui/codegen/equal-pointers-unequal/as-cast/segfault.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
//@ known-bug: #107975 | ||
//@ compile-flags: -Copt-level=2 | ||
//@ run-fail | ||
//@ check-run-results | ||
|
||
// This one should segfault. | ||
// I don't know a better way to check for segfault other than | ||
// check that it fails and that the output is empty. | ||
|
||
// https://github.com/rust-lang/rust/issues/107975#issuecomment-1431758601 | ||
|
||
use std::cell::RefCell; | ||
|
||
fn main() { | ||
let a = { | ||
let v = 0u8; | ||
&v as *const _ as usize | ||
}; | ||
let b = { | ||
let v = 0u8; | ||
&v as *const _ as usize | ||
}; | ||
let i = b - a; | ||
let arr = [ | ||
RefCell::new(Some(Box::new(1))), | ||
RefCell::new(None), | ||
RefCell::new(None), | ||
RefCell::new(None), | ||
]; | ||
assert_ne!(i, 0); | ||
let r = arr[i].borrow(); | ||
let r = r.as_ref().unwrap(); | ||
*arr[0].borrow_mut() = None; | ||
println!("{}", *r); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
//@ known-bug: #107975 | ||
//@ compile-flags: -Copt-level=2 | ||
//@ run-pass | ||
|
||
// Derived from https://github.com/rust-lang/rust/issues/107975#issuecomment-1431758601 | ||
|
||
fn main() { | ||
let a: usize = { | ||
let v = 0u8; | ||
&v as *const _ as usize | ||
}; | ||
let b: usize = { | ||
let v = 0u8; | ||
&v as *const _ as usize | ||
}; | ||
|
||
// So, are `a` and `b` equal? | ||
|
||
// Let's check their difference. | ||
let i: usize = a - b; | ||
// It's not zero, which means `a` and `b` are not equal. | ||
assert_ne!(i, 0); | ||
// But it looks like zero... | ||
assert_eq!(i.to_string(), "0"); | ||
// ...and now it *is* zero? | ||
assert_eq!(i, 0); | ||
// So `a` and `b` are equal after all? | ||
} |
25 changes: 25 additions & 0 deletions
25
tests/ui/codegen/equal-pointers-unequal/exposed-provenance/basic.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//@ known-bug: #107975 | ||
//@ compile-flags: -Copt-level=2 | ||
//@ run-pass | ||
|
||
#![feature(exposed_provenance)] | ||
|
||
use std::ptr::addr_of; | ||
|
||
fn main() { | ||
let a: usize = { | ||
let v = 0u8; | ||
addr_of!(v).expose_provenance() | ||
}; | ||
let b: usize = { | ||
let v = 0u8; | ||
addr_of!(v).expose_provenance() | ||
}; | ||
|
||
// `a` and `b` are not equal. | ||
assert_ne!(a, b); | ||
// But they are the same number. | ||
assert_eq!(format!("{a}"), format!("{b}")); | ||
// And they are equal. | ||
assert_eq!(a, b); | ||
} |
26 changes: 26 additions & 0 deletions
26
tests/ui/codegen/equal-pointers-unequal/exposed-provenance/function.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//@ known-bug: #107975 | ||
//@ compile-flags: -Copt-level=2 | ||
//@ run-pass | ||
|
||
// Based on https://github.com/rust-lang/rust/issues/107975#issuecomment-1434203908 | ||
|
||
#![feature(exposed_provenance)] | ||
|
||
use std::ptr::addr_of; | ||
|
||
fn f() -> usize { | ||
let v = 0; | ||
addr_of!(v).expose_provenance() | ||
} | ||
|
||
fn main() { | ||
let a = f(); | ||
let b = f(); | ||
|
||
// `a` and `b` are not equal. | ||
assert_ne!(a, b); | ||
// But they are the same number. | ||
assert_eq!(format!("{a}"), format!("{b}")); | ||
// And they are equal. | ||
assert_eq!(a, b); | ||
} |
35 changes: 35 additions & 0 deletions
35
tests/ui/codegen/equal-pointers-unequal/exposed-provenance/inline1.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
//@ known-bug: #107975 | ||
//@ compile-flags: -Copt-level=2 | ||
//@ run-pass | ||
//@ check-run-results | ||
//@ normalize-stdout-test: "\d+" -> "<..>" | ||
|
||
// Based on https://github.com/rust-lang/rust/issues/107975#issuecomment-1432161340 | ||
|
||
|
||
#![feature(exposed_provenance)] | ||
|
||
use std::ptr::addr_of; | ||
|
||
#[inline(never)] | ||
fn cmp(a: usize, b: usize) -> bool { | ||
a == b | ||
} | ||
|
||
#[inline(always)] | ||
fn cmp_in(a: usize, b: usize) -> bool { | ||
a == b | ||
} | ||
|
||
fn main() { | ||
let a = { | ||
let v = 0; | ||
addr_of!(v).expose_provenance() | ||
}; | ||
let b = { | ||
let v = 0; | ||
addr_of!(v).expose_provenance() | ||
}; | ||
println!("{a:?} == {b:?} -> ==: {}, cmp_in: {}, cmp: {}", a == b, cmp_in(a, b), cmp(a, b)); | ||
assert_eq!(a.to_string(), b.to_string()); | ||
} |
1 change: 1 addition & 0 deletions
1
tests/ui/codegen/equal-pointers-unequal/exposed-provenance/inline1.run.stdout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<..> == <..> -> ==: false, cmp_in: false, cmp: true |
34 changes: 34 additions & 0 deletions
34
tests/ui/codegen/equal-pointers-unequal/exposed-provenance/inline2.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//@ known-bug: #107975 | ||
//@ compile-flags: -Copt-level=2 | ||
//@ run-pass | ||
//@ check-run-results | ||
//@ normalize-stdout-test: "\d+" -> "<..>" | ||
|
||
// Based on https://github.com/rust-lang/rust/issues/107975#issuecomment-1432161340 | ||
|
||
#![feature(exposed_provenance)] | ||
|
||
use std::ptr::addr_of; | ||
|
||
#[inline(never)] | ||
fn cmp(a: usize, b: usize) -> bool { | ||
a == b | ||
} | ||
|
||
#[inline(always)] | ||
fn cmp_in(a: usize, b: usize) -> bool { | ||
a == b | ||
} | ||
|
||
fn main() { | ||
let a = { | ||
let v = 0; | ||
addr_of!(v).expose_provenance() | ||
}; | ||
let b = { | ||
let v = 0; | ||
addr_of!(v).expose_provenance() | ||
}; | ||
assert_eq!(a.to_string(), b.to_string()); | ||
println!("{a:?} == {b:?} -> ==: {}, cmp_in: {}, cmp: {}", a == b, cmp_in(a, b), cmp(a, b)); | ||
} |
1 change: 1 addition & 0 deletions
1
tests/ui/codegen/equal-pointers-unequal/exposed-provenance/inline2.run.stdout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<..> == <..> -> ==: true, cmp_in: true, cmp: true |
Oops, something went wrong.