Skip to content

CoercePointee doesn't tightly enforce the restriction to #[repr(transparent)]-only wrappers, if other proc-macros are involved #135206

Closed

Description

@steffahn

Define a proc macro that strips the #[repr(transparent)].

#[proc_macro_attribute]
pub fn untransparent(_: TokenStream, item: TokenStream) -> TokenStream {
    let mut i = item.into_iter();
    drop(i.next());
    drop(i.next());
    i.collect()
}

use it

#![feature(derive_coerce_pointee)]
use std::marker::CoercePointee;
use …:untransparent;

#[derive(CoercePointee)]
#[untransparent]
#[repr(transparent)]
struct Foo<T: ?Sized>(Box<T>);

compiles successfully, expands to

#![feature(prelude_import)]
#![feature(derive_coerce_pointee)]
#[prelude_import]
use std::prelude::rust_2021::*;
#[macro_use]
extern crate std;
use std::marker::CoercePointee;
use …::untransparent;
struct Foo<T: ?Sized>(Box<T>);
#[automatically_derived]
impl<
    T: ?Sized + ::core::marker::Unsize<__S>,
    __S: ?Sized,
> ::core::ops::DispatchFromDyn<Foo<__S>> for Foo<T> {}
#[automatically_derived]
impl<
    T: ?Sized + ::core::marker::Unsize<__S>,
    __S: ?Sized,
> ::core::ops::CoerceUnsized<Foo<__S>> for Foo<T> {}

As far as I understand the RFC and intent, the intended stabilization should only stabilize allowing these (CoerceUnsized & DispatchFromDyn impls) to be generated for #[repr(transparent)] types.

@rustbot label F-derive_coerce_pointee

Activity

  1. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    F-derive_coerce_pointeeFeature: RFC 3621's oft-renamed implementation
    on Jan 7, 2025
  2. changed the title [-]`CoercePointee` doesn't effectively enforce the restriction to `#[repr(transparent)]`-only wrappers[/-] [+]`CoercePointee` doesn't tightly enforce the restriction to `#[repr(transparent)]`-only wrappers, if other proc-macros are involved[/+] on Jan 7, 2025
  3. added
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Jan 7, 2025
  4. removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Jan 7, 2025
  5. dingxiangfei2009 commented on Jan 15, 2025

    @dingxiangfei2009
    Contributor

    Hey @steffahn and @BoxyUwU ! Thanks for spotting this!

    I was thinking the other day. Would it make sense that we enforce the repr(transparent) on the HIR typeck stage? I imagine that ...

    1. we could introduce a concrete but hidden trait CoercePointeeWellformed, like AsyncFnKindHelper. Name subject to scrutinee.
    2. in derive(CoercePointee), we add a impl<..> CoercePointeeWellformed for ...
    3. while checking coherence, we check validity of each impl<..> CoercePointeeWellformed for ... For this issue, it will check the field and shape of the Self type in perspective impls.

    Apologies ahead for using wrong terms and please don't hesitate to correct me.

    cc @Darksonn

  6. steffahn commented on Jan 15, 2025

    @steffahn
    MemberAuthor

    My ”dumb idea“ personally here would be: could validation of the DispatchFromDyn (the code that already makes sure that there’s only a single non-ZST field – recently upgraded to single non-PhantomData) impl simply also enforce the #[repr(transparent)] (on the struct) if there’s #[automatically_derived] on it (on the impl)?

  7. Darksonn commented on Jan 16, 2025

    @Darksonn
    Member

    Checking #[automatically_derived] sounds like it would take fewer lines of code, but it also sounds more hacky, so I'm not sure it's the best approach.

    @compiler-errors In the fix you wanted to make for #135217, are you planning to introduce a new CoercePointeeWellFormed trait? If so, then it sounds like there's an opportunity to use the same trait for both checks.

  8. dingxiangfei2009 commented on Jan 21, 2025

    @dingxiangfei2009
    Contributor

    I am experimenting the WF trait since this week. I will report back with results.

    @rustbot claim

  9. added 3 commits that reference this issue on Feb 10, 2025
    fa8158a
    ebdf4b0
    af3c51d
  10. added a commit that references this issue on Feb 11, 2025
    368a423
  11. added 2 commits that reference this issue on Mar 11, 2025
    bdd68ec
    ea86b6b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

C-bugCategory: This is a bug.F-derive_coerce_pointeeFeature: RFC 3621's oft-renamed implementationT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions