Skip to content

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

Closed

Description

@RalfJung

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*...

Activity

  1. RalfJung commented on Feb 5, 2023

    @RalfJung
    MemberAuthor

    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.

  2. 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[/+] on Feb 6, 2023
  3. added 2 commits that reference this issue on Feb 8, 2023
    7ae3ccf
    fabefe3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    F-dyn_star`#![feature(dyn_star)]`

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions