Skip to content

Commit

Permalink
https://github.com/rust-lang/rust/issues/103763
Browse files Browse the repository at this point in the history
  • Loading branch information
YoungHaKim7 committed Sep 16, 2023
1 parent 4cc46c3 commit dd36e62
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 0 deletions.
15 changes: 15 additions & 0 deletions rust1_72/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/target
.DS_Store
/.vscode
# Generated by Cargo
# will have compiled files and executables
debug/
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
dist/
pkg/
5 changes: 5 additions & 0 deletions rust1_72/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Result

```
```
15 changes: 15 additions & 0 deletions rust1_72/arc_ptr_eq/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/target
.DS_Store
/.vscode
# Generated by Cargo
# will have compiled files and executables
debug/
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
dist/
pkg/
8 changes: 8 additions & 0 deletions rust1_72/arc_ptr_eq/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "arc_ptr_eq"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
26 changes: 26 additions & 0 deletions rust1_72/arc_ptr_eq/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Result

https://github.com/rust-lang/rust/issues/103763

```
$ cargo run
Compiling arc_ptr_eq v0.1.0 (/Users/globalyoung/Documents/test/test/rust/rust_release/rust1_72/arc_ptr_eq)
Finished dev [unoptimized + debuginfo] target(s) in 0.10s
Running `target/debug/arc_ptr_eq`
Found one!
[src/main.rs:23] a_data_addr = 0x000000016ee2a6a0
[src/main.rs:24] b_data_addr = 0x000000016ee2a6a0
[src/main.rs:25] a = 0x000000016ee2a6a0
[src/main.rs:26] b = 0x000000016ee2a6a0
[src/main.rs:27] a_msg = "Zst"
[src/main.rs:28] b_msg = "Msg(good gravy)"
[src/main.rs:37] (*zm.0).type_id() = TypeId {
t: 177224103700599114721518439526924153296,
}
[src/main.rs:38] (*zm.1).type_id() = TypeId {
t: 18653999097659511391617033891817030262,
}
```
53 changes: 53 additions & 0 deletions rust1_72/arc_ptr_eq/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use std::any::Any;

trait Trait: Any {
fn msg(&self) -> String;
}

fn pair(a: *const dyn Trait, b: *const dyn Trait) {
let a_msg;
let b_msg;
unsafe {
a_msg = a.as_ref().unwrap().msg();
b_msg = b.as_ref().unwrap().msg();
}

let a_data_addr = a as *const ();
let b_data_addr = b as *const ();

// The question: for non-null a, b,
// if `a_data_addr == b_data_addr`, can you ever have `a_msg != b_msg` ?

if a_msg != b_msg && a_data_addr == b_data_addr {
println!("Found one!");
dbg!(a_data_addr);
dbg!(b_data_addr);
dbg!(a);
dbg!(b);
dbg!(a_msg);
dbg!(b_msg);
}
}

fn main() {
let zm = (Zst, Msg("good gravy"));
let zm: (&dyn Trait, &dyn Trait) = (&zm.0, &zm.1);
pair(zm.0, zm.1);

dbg!((*zm.0).type_id());
dbg!((*zm.1).type_id());
}

struct Zst;
struct Msg(&'static str);

impl Trait for Zst {
fn msg(&self) -> String {
format!("Zst")
}
}
impl Trait for Msg {
fn msg(&self) -> String {
format!("Msg({})", self.0)
}
}

0 comments on commit dd36e62

Please sign in to comment.