Stream: t-compiler/wg-rls-2.0

Topic: question


std::Veetaha (Jan 16 2020 at 20:43, on Zulip):

Can someone please explain what is the difference between flatten_mut() and flatten_const() methods in this code? I see that both of them have the same income and outcome.

std::Veetaha (Jan 16 2020 at 20:43, on Zulip):

https://github.com/CAD97/thin-dst/blob/1da8b7ac72829b1ed57075d13810bc2ce9842e0b/src/lib.rs#L111

pksunkara (Jan 16 2020 at 21:13, on Zulip):

but fatten_mut uses make_slice_mut which probably cuts things from the given pointer

std::Veetaha (Jan 16 2020 at 21:15, on Zulip):

Those are just wrappers for slice::from_raw_parts() and slice::from_raw_parts_mut()
https://github.com/CAD97/thin-dst/blob/1da8b7ac72829b1ed57075d13810bc2ce9842e0b/src/polyfill.rs#L9

matklad (Jan 17 2020 at 09:19, on Zulip):

They give different pointer provenance. One goes via &, the other via &mut. If you intend to mutate the thing, you need to get the raw pointer from an &mut.

Joshua Nelson (Jan 17 2020 at 16:55, on Zulip):

provenance?

matklad (Jan 17 2020 at 16:57, on Zulip):

yup: https://github.com/rust-lang/unsafe-code-guidelines/issues/52, https://github.com/rust-lang/rust/pull/54461

Basically, it's only valid to write through *mut if it originated (has a provenance of?) from a mutable location

Joshua Nelson (Jan 17 2020 at 16:59, on Zulip):

oh, so like *(&t as *const _ as *mut) = whatever is invalid

matklad (Jan 17 2020 at 17:01, on Zulip):

right

matklad (Jan 17 2020 at 17:01, on Zulip):

well, I don't know a lot about this stuff myself, but that's my semi-layman understanding

Joshua Nelson (Jan 17 2020 at 17:03, on Zulip):

let me go change some code at $WORK

std::Veetaha (Jan 17 2020 at 23:38, on Zulip):

@matklad , wow that is something I never knew, this provenance thing is something complex. I've read this post and if I understood it correct provenance is not something that is standardized but instead used exclusively by the compiler. There is also a post that justifies that pointers are not just integers. I'd make a conclusion that provenance is the static metadata used for optimizations, that is erased in the compiled output, and at runtime, pointers are indeed just integers.

Christopher Durham (Jan 25 2020 at 22:06, on Zulip):

I wrote a good bit over on the issue you posted. It is about pointer provenance, but the real issue is that make_mut/make_slice_mut necessarily create references, and going through the references puts

std::Veetaha (Jan 26 2020 at 11:07, on Zulip):

@Christopher Durham , if you project is open-source the link to it and the place where you put { would be helpful. Or otherwise maybe some excerpt of code which triggers this crash

std::Veetaha (Jan 26 2020 at 11:16, on Zulip):

@Christopher Durham, thank you for the detailed answer! While trying to understand the pointer cast from *const [SliceItem] to NonNull<ThinData<Head, SliceItem>> I cloned your repo and ran cargo build, but it failed:

$ rustc --version
rustc 1.40.0 (73528e339 2019-12-16)
$ cargo build
  Compiling thin-dst v1.0.0 (/home/veetaha/my/junk/thin-dst)
error[E0210]: type parameter `Head` must be used as the type parameter for some local type (e.g., `MyStruct<Head>`)
   --> src/lib.rs:433:6
    |
433 | impl<Head, SliceItem> From<ThinArc<Head, SliceItem>> for Arc<ThinData<Head, SliceItem>> {
    |      ^^^^ type parameter `Head` must be used as the type parameter for some local type
    |
    = note: only traits defined in the current crate can be implemented for a type parameter

error[E0210]: type parameter `Head` must be used as the type parameter for some local type (e.g., `MyStruct<Head>`)
   --> src/lib.rs:492:6
    |
492 | impl<Head, SliceItem> From<ThinRc<Head, SliceItem>> for Rc<ThinData<Head, SliceItem>> {
    |      ^^^^ type parameter `Head` must be used as the type parameter for some local type
    |
    = note: only traits defined in the current crate can be implemented for a type parameter

error[E0210]: type parameter `Head` must be used as the type parameter for some local type (e.g., `MyStruct<Head>`)
   --> src/lib.rs:574:6
    |
574 | impl<Head, SliceItem> From<ThinPtr<Head, SliceItem>> for NonNull<ThinData<Head, SliceItem>> {
    |      ^^^^ type parameter `Head` must be used as the type parameter for some local type
    |
    = note: only traits defined in the current crate can be implemented for a type parameter

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0210`.
error: could not compile `thin-dst`.

To learn more, run the command again with --verbose.
~/my/junk/thin-dst (master) $ rustc --version
rustc 1.40.0 (73528e339 2019-12-16)
~/my/junk/thin-dst (master) $ cargo miri test
error: 'cargo-miri' is not installed for the toolchain 'stable-x86_64-unknown-linux-gnu'
To install, run `rustup component add miri`
~/my/junk/thin-dst (master) $ rustup component add miri
error: component 'miri' for target 'x86_64-unknown-linux-gnu' is unavailable for download for channel stable
~/my/junk/thin-dst (master) $ cd ..
~/my/junk $ rm -rf thin-dst/
~/my/junk $ git clone https://github.com/CAD97/thin-dst.git
Cloning into 'thin-dst'...
remote: Enumerating objects: 125, done.
remote: Counting objects: 100% (125/125), done.
remote: Compressing objects: 100% (76/76), done.
remote: Total 125 (delta 46), reused 106 (delta 35), pack-reused 0
Receiving objects: 100% (125/125), 40.70 KiB | 541.00 KiB/s, done.
Resolving deltas: 100% (46/46), done.
~/my/junk $ code thin-dst/
~/my/junk $ cd thin-dst/
~/my/junk/thin-dst (master) $ ls
Cargo.toml  CHANGELOG.md  LICENSE  README.md  rustfmt.toml  src  tests
~/my/junk/thin-dst (master) $ cargo build
   Compiling thin-dst v1.0.0 (/home/veetaha/my/junk/thin-dst)
error[E0210]: type parameter `Head` must be used as the type parameter for some local type (e.g., `MyStruct<Head>`)
   --> src/lib.rs:433:6
    |
433 | impl<Head, SliceItem> From<ThinArc<Head, SliceItem>> for Arc<ThinData<Head, SliceItem>> {
    |      ^^^^ type parameter `Head` must be used as the type parameter for some local type
    |
    = note: only traits defined in the current crate can be implemented for a type parameter

error[E0210]: type parameter `Head` must be used as the type parameter for some local type (e.g., `MyStruct<Head>`)
   --> src/lib.rs:492:6
    |
492 | impl<Head, SliceItem> From<ThinRc<Head, SliceItem>> for Rc<ThinData<Head, SliceItem>> {
    |      ^^^^ type parameter `Head` must be used as the type parameter for some local type
    |
    = note: only traits defined in the current crate can be implemented for a type parameter

error[E0210]: type parameter `Head` must be used as the type parameter for some local type (e.g., `MyStruct<Head>`)
   --> src/lib.rs:574:6
    |
574 | impl<Head, SliceItem> From<ThinPtr<Head, SliceItem>> for NonNull<ThinData<Head, SliceItem>> {
    |      ^^^^ type parameter `Head` must be used as the type parameter for some local type
    |
    = note: only traits defined in the current crate can be implemented for a type parameter

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0210`.
error: could not compile `thin-dst`.
Christopher Durham (Jan 26 2020 at 14:06, on Zulip):

Yeah, head has those two impls that only work on beta currently

Christopher Durham (Jan 26 2020 at 14:08, on Zulip):

1.41 adjusts coherence rules a bit so those impls are considered local.

Last update: Jun 07 2020 at 10:25UTC