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

dyn-star: "needs to be a pointer-sized type" but the type is pointer-sized #107696

Closed
RalfJung opened this issue Feb 5, 2023 · 1 comment · Fixed by #107769
Closed

dyn-star: "needs to be a pointer-sized type" but the type is pointer-sized #107696

RalfJung opened this issue Feb 5, 2023 · 1 comment · Fixed by #107769
Labels
F-dyn_star `#![feature(dyn_star)]`

Comments

@RalfJung
Copy link
Member

RalfJung commented Feb 5, 2023

Consider this code:

#![feature(dyn_star)]
#![allow(incomplete_features)]

use std::fmt::Debug;

fn main() {
    let _dyn_i: dyn* Debug = [0u8; std::mem::size_of::<usize>()];
}

This raises an error:

error[E0277]: `[u8; 8]` needs to be a pointer-sized type

However, it is a pointer-sized type. Looks like somehow that's still not good enough?

On a related note, I am surprised that this PointerSize marker trait (according to its documentation) cares only about size and not alignment. I think things would go very wrong if we had a type that had ptr size but larger alignment than a ptr, and then turned that into a dyn*...

@RalfJung
Copy link
Member Author

RalfJung commented Feb 5, 2023

I think I found the code implementing this:

fn consider_builtin_pointer_sized_candidate(
ecx: &mut EvalCtxt<'_, 'tcx>,
goal: Goal<'tcx, Self>,
) -> QueryResult<'tcx> {
if goal.predicate.self_ty().has_non_region_infer() {
return ecx.make_canonical_response(Certainty::AMBIGUOUS);
}
let tcx = ecx.tcx();
let self_ty = tcx.erase_regions(goal.predicate.self_ty());
if let Ok(layout) = tcx.layout_of(goal.param_env.and(self_ty))
&& let usize_layout = tcx.layout_of(ty::ParamEnv::empty().and(tcx.types.usize)).unwrap().layout
&& layout.layout.size() == usize_layout.size()
&& layout.layout.align().abi == usize_layout.align().abi
{
// FIXME: We could make this faster by making a no-constraints response
ecx.make_canonical_response(Certainty::Yes)
} else {
Err(NoSolution)
}
}

So actually, this trait is about the type having both the size and alignment of a pointer type. That resolves my alignment-related concerns, but means that the doc comment and error message associated with PointerSize (and its name!) are missing part of the story.

@RalfJung RalfJung changed the title "needs to be a pointer-sized type" but the type is pointer-sized dyn-star: "needs to be a pointer-sized type" but the type is pointer-sized Feb 6, 2023
@RalfJung RalfJung added the F-dyn_star `#![feature(dyn_star)]` label Feb 6, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 8, 2023
Rename `PointerSized` to `PointerLike`

The old name was unnecessarily vague. This PR renames a nightly language feature that I added, so I don't think it needs any additional approval, though anyone can feel free to speak up if you dislike the rename.

It's still unsatisfying that we don't the user which of {size, alignment} is wrong, but this trait really is just a stepping stone for a more generalized mechanism to create `dyn*`, just meant for nightly testing, so I don't think it really deserves additional diagnostic machinery for now.

Fixes rust-lang#107696, cc `@RalfJung`
r? `@eholk`
@bors bors closed this as completed in fabefe3 Feb 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F-dyn_star `#![feature(dyn_star)]`
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant