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

ptr-ptr cast does not work well with lifetimes (lifetime widening) #27214

Closed
arielb1 opened this issue Jul 22, 2015 · 9 comments
Closed

ptr-ptr cast does not work well with lifetimes (lifetime widening) #27214

arielb1 opened this issue Jul 22, 2015 · 9 comments
Labels
A-coercions Area: implicit and explicit `expr as Type` coercions A-lifetimes Area: Lifetimes / regions A-raw-pointers Area: raw pointers, MaybeUninit, NonNull A-type-system Area: Type system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@arielb1
Copy link
Contributor

arielb1 commented Jul 22, 2015

STR

fn foo<'a>(v: *const &'a ()) {
    let v: *const &'static () = v as *const &'static ();
}

fn main() {}

Expected result

cast will be successful

Actual Result

<anon>:2:33: 2:34 error: mismatched types:
 expected `*const &'static ()`,
    found `*const &'a ()`
(lifetime mismatch) [E0308]
<anon>:2     let v: *const &'static () = v as *const &'static ();
                                         ^
<anon>:2:33: 2:34 help: see the detailed explanation for E0308
<anon>:1:30: 3:2 note: the lifetime 'a as defined on the block at 1:29...
<anon>:1 fn foo<'a>(v: *const &'a ()) {
<anon>:2     let v: *const &'static () = v as *const &'static ();
<anon>:3 }
note: ...does not necessarily outlive the static lifetime
error: aborting due to previous error
playpen: application terminated with error code 101
@steveklabnik steveklabnik added the A-type-system Area: Type system label Jul 22, 2015
@steveklabnik
Copy link
Member

steveklabnik commented Nov 30, 2016

Why would this cast work? 'a can be shorter than 'static, as the error says.

@bluss
Copy link
Member

bluss commented Dec 1, 2016

Raw pointer casts are normally not restricted. *const A as *const B is usally ok, without any checking of any compatibility between A and B.

@steveklabnik
Copy link
Member

Ah, I see, thanks @bluss . Seems reasonable.

@ranma42
Copy link
Contributor

ranma42 commented Dec 1, 2016

Does Rust guarantee that pointers are correctly aligned? Would it be legal in a safe context to create a misaligned pointer?
Neither https://doc.rust-lang.org/book/raw-pointers.html nor https://doc.rust-lang.org/book/unsafe.html mention any constraint on alignment.

@bluss
Copy link
Member

bluss commented Dec 1, 2016

Currently, loads and writes through raw pointer dereferences assume the pointer is aligned (this is guarded by unsafe).

Box::new(t) creates a 0x1 pointer if t is zero size, even if it needs more than 1-alignment. For example if it is a [u64; 0] (size 0, align of 8). From the box you can thus get a misaligned reference, however it is one that can't be read/written through (can't read/write something of 0 size).

For guarantees, have to ask https://internals.rust-lang.org/c/language-design/unsafe-code-guidelines

@Rufflewind
Copy link
Contributor

As @talchas on #rust pointed out to me, as seems to forbid lifetime-only changes, but if anything else changes it's totally on board:

fn foo<'a>(v: *const &'a ()) {
    let v: *const &'static () = v as *const () as *const &'static ();
}

@Mark-Simulacrum Mark-Simulacrum added the C-bug Category: This is a bug. label Jul 22, 2017
@steveklabnik
Copy link
Member

Triage: still reproduces.

@Noratrieb Noratrieb added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Apr 5, 2023
@fmease fmease added A-lifetimes Area: Lifetimes / regions A-coercions Area: implicit and explicit `expr as Type` coercions A-raw-pointers Area: raw pointers, MaybeUninit, NonNull labels Sep 3, 2024
@fmease
Copy link
Member

fmease commented Sep 3, 2024

Triage: No longer reproduces (fail→pass). We could just close this as completed but I'm actually not sure which PR(s) was responsible. Might try to bisect later.

@fmease fmease changed the title ptr-ptr cast does not work well with lifetimes ptr-ptr cast does not work well with lifetimes (lifetime widening) Sep 3, 2024
@fmease
Copy link
Member

fmease commented Nov 23, 2024

Bisects to 1.75 / nightly-2023-10-27 / PR #113262.
See also issue #113257 which duplicates this one.

Closing as fixed by #113262. No additional tests needed.

@fmease fmease closed this as completed Nov 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-coercions Area: implicit and explicit `expr as Type` coercions A-lifetimes Area: Lifetimes / regions A-raw-pointers Area: raw pointers, MaybeUninit, NonNull A-type-system Area: Type system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

8 participants