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

In const fn in a macro call, raw ptr deref does not work even inside unsafe #75340

Closed
RalfJung opened this issue Aug 9, 2020 · 14 comments
Closed
Labels
A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

Comments

@RalfJung
Copy link
Member

RalfJung commented Aug 9, 2020

The following code should build, but for some reason it does not:

#![feature(const_raw_ptr_deref)]
#![feature(raw_ref_macros)]

use std::ptr;

const fn test_fn(x: *const i32) {
    let x2 = unsafe { ptr::raw_const!(*x) };
}

Removing the const or adding a const_fn feature gate makes it build.

Cc @oli-obk @petrochenkov

@RalfJung RalfJung changed the title In const fn with macro, raw ptr deref does not work even inside unsafe In const fn in a macro call, raw ptr deref does not work even inside unsafe Aug 9, 2020
@RalfJung
Copy link
Member Author

RalfJung commented Aug 9, 2020

Ah, adding the const_fn feature also helps... is that the expected behavior @oli-obk?

@oli-obk
Copy link
Contributor

oli-obk commented Aug 10, 2020

Yes and no. The MIR is &raw const (*_1), where _1 is x. We prevent the deref on the raw pointer, even if that's not an actual read. Basically it's a combination of

UnsafetyViolationDetails::DerefOfRawPointer,
and
UnsafetyViolationKind::General

So... I guess we need to change

UnsafetyViolationKind::General,
to GeneralAndConstFn

@oli-obk oli-obk added A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. labels Aug 11, 2020
@mlodato517
Copy link
Contributor

mlodato517 commented Aug 11, 2020

Has this ticket been picked up by anyone? If not, I'd be happy to hop in (especially if it's as easy as changing that one argument!)

@CloudNClock
Copy link

CloudNClock commented Aug 11, 2020

Has this ticket been picked up by anyone? If not, I'd be happy to hop in (especially if it's as easy as changing that one argument!)

@mlodato517 Hello, can I take this issue? This is my first time trying to make a PR. Thank you.

@mlodato517
Copy link
Contributor

That's okay with me (unless someone got here before both of us and took it 😉)

@CloudNClock

This comment has been minimized.

@oli-obk

This comment has been minimized.

@RalfJung

This comment has been minimized.

@oli-obk

This comment has been minimized.

@RalfJung

This comment has been minimized.

@RalfJung
Copy link
Member Author

@5M1Sec please post PR-specific questions in the PR, not the issue. That just mixes up the discussion about a particular fix and the discussion about the problem in general -- very confusing. Instead, leave a message here to point out that a PR exists, or ping people on the PR.

I hid your comment here to avoid that confusion.

JohnTitor added a commit to JohnTitor/rust that referenced this issue Aug 18, 2020
Allowing raw ptr dereference in const fn

Reflect on issue rust-lang#75340
Discussion in previous PR  rust-lang#75425

## Updates
Change `UnsafetyViolationKind::General` to `UnsafetyViolationKind::GeneralAndConstFn` in check_unsafety.rs

Remove `unsafe` in min_const_fn_unsafe_bad.rs

Bless min_const_fn

Add the test case from issue 75340
***
Sorry for the chaos. I messed up and ended up deleting the repo in the last PR. I have to create a new PR for the new repo. I will make a feature branch next time. I will edit the old PR once I receive the commends.

@RalfJung Thank you all for your replies. They are helpful!

r? @oli-obk
@RalfJung
Copy link
Member Author

With #75578 having landed, is there anything left to do here?

@oli-obk
Copy link
Contributor

oli-obk commented Aug 18, 2020

depends on whether we want this to work without the const_raw_ptr_deref feature gate. It's not actually a deref, it's a raw pointer reborrow of sorts.

@RalfJung
Copy link
Member Author

RalfJung commented Aug 18, 2020

For me, "deref" is the unary * operator. That's also how MIR places use that term. So the gate makes sense for that.

Closing then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
Projects
None yet
Development

No branches or pull requests

4 participants