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

Don't allow the #[pointee] attribute where it doesn't belong #128721

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Brezak
Copy link

@Brezak Brezak commented Aug 6, 2024

Error if the #[pointee] attribute is applied to anything but generic type parameters.

Closes #128485
Related to #123430

@rustbot
Copy link
Collaborator

rustbot commented Aug 6, 2024

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @pnkfelix (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 6, 2024
@Darksonn
Copy link
Contributor

Darksonn commented Aug 6, 2024

@rustbot label F-derive_smart_pointer

@rustbot rustbot added the F-derive_smart_pointer `#![feature(derive_smart_pointer)]` label Aug 6, 2024
@oli-obk
Copy link
Contributor

oli-obk commented Aug 6, 2024

This still allows type Foo<#[pointee] T> = T; I think? It should only be legal below a smart pointer derive. Maybe check how derive(Default) handles the default enum variant attribute

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@Darksonn
Copy link
Contributor

PR #128925 was opened to fix #128888, but it may also have fixed the issue this PR is trying to fix.

@Brezak
Copy link
Author

Brezak commented Aug 15, 2024

This PR also adds a check that [#pointee] is only attached to a generic parameter. Should I reduce this PR to just that, or close it and open a new one?

@Darksonn
Copy link
Contributor

That change could still make sense.

@bors
Copy link
Contributor

bors commented Aug 17, 2024

☔ The latest upstream changes (presumably #129202) made this pull request unmergeable. Please resolve the merge conflicts.

@Brezak
Copy link
Author

Brezak commented Aug 20, 2024

I've shrunk the PR down to just checking if the attribute is applied to a generic type parameter.

@bors
Copy link
Contributor

bors commented Aug 29, 2024

☔ The latest upstream changes (presumably #129721) made this pull request unmergeable. Please resolve the merge conflicts.


fn visit_generic_param(&mut self, param: &'a rustc_ast::GenericParam) -> Self::Result {
match param.kind {
GenericParamKind::Type { .. } => return,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm.

Technically I think you still need to ensure you recur into the default: &Option<P<Ty>> that's held within that enum payload above.

I think the below is the kind of case you need to worry about that motivates recurring here:

struct SomeStruct<'a, T = [u32; const { #[pointee] struct UhOhYetAnotherTypeDefn<U>; 10 }]> { ... }

(Yes its a corner case.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can model the recursion you need to do by looking at walk_generic_param, and just leaving out the visit_attribute part:

pub fn walk_generic_param<'a, V: Visitor<'a>>(
visitor: &mut V,
param: &'a GenericParam,
) -> V::Result {
let GenericParam { id: _, ident, attrs, bounds, is_placeholder: _, kind, colon_span: _ } =
param;
walk_list!(visitor, visit_attribute, attrs);
try_visit!(visitor.visit_ident(*ident));
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
match kind {
GenericParamKind::Lifetime => (),
GenericParamKind::Type { default } => visit_opt!(visitor, visit_ty, default),
GenericParamKind::Const { ty, default, kw_span: _ } => {
try_visit!(visitor.visit_ty(ty));
visit_opt!(visitor, visit_anon_const, default);
}
}
V::Result::output()
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've fixed the edge case. As a comment in the new code mentions we end up rejecting valid code like:

#![feature(derive_smart_pointer)]
use std::marker::SmartPointer;
#[derive(SmartPointer)]
#[repr(transparent)]
struct Weird<
    'a,
    T: ?Sized,
    const V: u32 = {
        #[derive(SmartPointer)]
        #[repr(transparent)]
        struct UhOh<'b, #[pointee] X: ?Sized>(&'b X);
        10
    },
> {
    ptr: &'a T,
}

Handling this properly isn't possible in the generic case since we can't know if the inner derive is actually a different trait with the same name. Given how unlikely the whole scenario is I've decided to go ahead with the changes anyway.

@pnkfelix
Copy link
Member

pnkfelix commented Oct 4, 2024

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 4, 2024
@rust-log-analyzer

This comment has been minimized.

@Brezak
Copy link
Author

Brezak commented Oct 6, 2024

@rustbot review

@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Oct 6, 2024
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 6, 2024
@pnkfelix
Copy link
Member

pnkfelix commented Oct 7, 2024

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Oct 7, 2024

📌 Commit aa4f16a has been approved by pnkfelix

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F-derive_smart_pointer `#![feature(derive_smart_pointer)]` S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

#[pointee] attribute can be applied to anything
7 participants