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

Merged
merged 1 commit into from
Oct 7, 2024

Conversation

Brezak
Copy link
Contributor

@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_coerce_pointee Feature: RFC 3621's oft-renamed implementation 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.

@Brezak Brezak force-pushed the pointee-in-strange-places branch from db05a4d to 027b6db Compare August 6, 2024 17:30
@rust-log-analyzer

This comment has been minimized.

@Brezak Brezak force-pushed the pointee-in-strange-places branch from 027b6db to 4043d90 Compare August 6, 2024 19:43
@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
Contributor 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 Brezak force-pushed the pointee-in-strange-places branch from 4043d90 to fcdb5f0 Compare August 20, 2024 20:51
@Brezak
Copy link
Contributor 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.

@Brezak Brezak force-pushed the pointee-in-strange-places branch from fcdb5f0 to a26d609 Compare September 4, 2024 14:36

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
Contributor 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
@Brezak Brezak force-pushed the pointee-in-strange-places branch from a26d609 to cc9d8b5 Compare October 6, 2024 21:15
@rust-log-analyzer

This comment has been minimized.

@Brezak Brezak force-pushed the pointee-in-strange-places branch from cc9d8b5 to aa4f16a Compare October 6, 2024 21:56
@Brezak
Copy link
Contributor 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
bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 7, 2024
…kingjubilee

Rollup of 7 pull requests

Successful merges:

 - rust-lang#128721 (Don't allow the `#[pointee]` attribute where it doesn't belong)
 - rust-lang#130479 (skip in-tree compiler build for llvm-bitcode-linker if ci-rustc is on)
 - rust-lang#130899 (Couple of changes to make it easier to compile rustc for wasm)
 - rust-lang#131225 (`rustc_borrowck` memory management tweaks)
 - rust-lang#131351 (Remove valgrind test suite and support from compiletest, bootstrap and opt-dist)
 - rust-lang#131359 (Fix used_underscore_binding in rustc_serialize)
 - rust-lang#131367 (Mark Boxy as on vacation)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit bd2e7ee into rust-lang:master Oct 7, 2024
6 checks passed
@rustbot rustbot added this to the 1.83.0 milestone Oct 7, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Oct 7, 2024
Rollup merge of rust-lang#128721 - Brezak:pointee-in-strange-places, r=pnkfelix

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

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

Closes rust-lang#128485
Related to rust-lang#123430
@Brezak Brezak deleted the pointee-in-strange-places branch October 7, 2024 21:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F-derive_coerce_pointee Feature: RFC 3621's oft-renamed implementation 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