Stream: t-compiler/rust-analyzer

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 correctly 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.
But I am not sure how this is applicable to the context I gave initially. This seems to be used only for detecting pointers aliasing I am not sure how this applies to *mut and *const context.

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):

Sorry for the junk dir, it is actually the place where I test things out )

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:

~/my/junk/thin-dst (master) $ rustc --version
rustc 1.40.0 (73528e339 2019-12-16)
~/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: Jul 24 2021 at 21:15UTC