Skip to content

Commit 8b2f6f8

Browse files
committed
Don't allow the #[pointee] attribute where it doesn't belong
1 parent 9179d9b commit 8b2f6f8

File tree

6 files changed

+109
-2
lines changed

6 files changed

+109
-2
lines changed

compiler/rustc_hir/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub mod intravisit;
2626
pub mod lang_items;
2727
pub mod pat_util;
2828
mod stable_hash_impls;
29-
mod target;
29+
pub mod target;
3030
pub mod weak_lang_items;
3131

3232
#[cfg(test)]

compiler/rustc_passes/messages.ftl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,10 @@ passes_pass_by_value =
584584
`pass_by_value` attribute should be applied to a struct, enum or type alias
585585
.label = is not a struct, enum or type alias
586586
587+
passes_pointee_on_non_generic_ty =
588+
attribute should be applied to generic type parameters
589+
.label = not a generic type parameter
590+
587591
passes_proc_macro_bad_sig = {$kind} has incorrect signature
588592
589593
passes_remove_fields =

compiler/rustc_passes/src/check_attr.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
242242
[sym::coroutine, ..] => {
243243
self.check_coroutine(attr, target);
244244
}
245+
[sym::pointee, ..] => {
246+
self.check_pointee(attr, target);
247+
}
245248
[
246249
// ok
247250
sym::allow
@@ -253,7 +256,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
253256
// need to be fixed
254257
| sym::cfi_encoding // FIXME(cfi_encoding)
255258
| sym::may_dangle // FIXME(dropck_eyepatch)
256-
| sym::pointee // FIXME(derive_smart_pointer)
257259
| sym::linkage // FIXME(linkage)
258260
| sym::no_sanitize // FIXME(no_sanitize)
259261
| sym::omit_gdb_pretty_printer_section // FIXME(omit_gdb_pretty_printer_section)
@@ -2343,6 +2345,15 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
23432345
}
23442346
}
23452347
}
2348+
2349+
fn check_pointee(&self, attr: &Attribute, target: Target) {
2350+
match target {
2351+
Target::GenericParam(hir::target::GenericParamKind::Type) => return,
2352+
_ => {
2353+
self.dcx().emit_err(errors::PointeeOnNonGenericTy { attr_span: attr.span });
2354+
}
2355+
}
2356+
}
23462357
}
23472358

23482359
impl<'tcx> Visitor<'tcx> for CheckAttrVisitor<'tcx> {

compiler/rustc_passes/src/errors.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,13 @@ pub struct CoroutineOnNonClosure {
643643
pub span: Span,
644644
}
645645

646+
#[derive(Diagnostic)]
647+
#[diag(passes_pointee_on_non_generic_ty)]
648+
pub struct PointeeOnNonGenericTy {
649+
#[primary_span]
650+
pub attr_span: Span,
651+
}
652+
646653
#[derive(Diagnostic)]
647654
#[diag(passes_empty_confusables)]
648655
pub(crate) struct EmptyConfusables {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#![feature(derive_smart_pointer)]
2+
3+
#[pointee]
4+
//~^ ERROR: attribute should be applied to generic type parameters
5+
struct AStruct<
6+
#[pointee]
7+
//~^ ERROR: attribute should be applied to generic type parameters
8+
'lifetime,
9+
#[pointee]
10+
//~^ ERROR: attribute should be applied to generic type parameters
11+
const CONST: usize
12+
> {
13+
#[pointee]
14+
//~^ ERROR: attribute should be applied to generic type parameters
15+
val: &'lifetime ()
16+
}
17+
18+
#[pointee]
19+
//~^ ERROR: attribute should be applied to generic type parameters
20+
enum AnEnum {
21+
#[pointee]
22+
//~^ ERROR: attribute should be applied to generic type parameters
23+
AVariant
24+
}
25+
26+
#[pointee]
27+
//~^ ERROR: attribute should be applied to generic type parameters
28+
mod AModule {}
29+
30+
#[pointee]
31+
//~^ ERROR: attribute should be applied to generic type parameters
32+
fn a_function(
33+
) {}
34+
35+
fn main() {}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
error: attribute should be applied to generic type parameters
2+
--> $DIR/deriving-smart-pointer-pointee-in-strange-places.rs:3:1
3+
|
4+
LL | #[pointee]
5+
| ^^^^^^^^^^
6+
7+
error: attribute should be applied to generic type parameters
8+
--> $DIR/deriving-smart-pointer-pointee-in-strange-places.rs:6:5
9+
|
10+
LL | #[pointee]
11+
| ^^^^^^^^^^
12+
13+
error: attribute should be applied to generic type parameters
14+
--> $DIR/deriving-smart-pointer-pointee-in-strange-places.rs:9:5
15+
|
16+
LL | #[pointee]
17+
| ^^^^^^^^^^
18+
19+
error: attribute should be applied to generic type parameters
20+
--> $DIR/deriving-smart-pointer-pointee-in-strange-places.rs:13:5
21+
|
22+
LL | #[pointee]
23+
| ^^^^^^^^^^
24+
25+
error: attribute should be applied to generic type parameters
26+
--> $DIR/deriving-smart-pointer-pointee-in-strange-places.rs:18:1
27+
|
28+
LL | #[pointee]
29+
| ^^^^^^^^^^
30+
31+
error: attribute should be applied to generic type parameters
32+
--> $DIR/deriving-smart-pointer-pointee-in-strange-places.rs:21:5
33+
|
34+
LL | #[pointee]
35+
| ^^^^^^^^^^
36+
37+
error: attribute should be applied to generic type parameters
38+
--> $DIR/deriving-smart-pointer-pointee-in-strange-places.rs:26:1
39+
|
40+
LL | #[pointee]
41+
| ^^^^^^^^^^
42+
43+
error: attribute should be applied to generic type parameters
44+
--> $DIR/deriving-smart-pointer-pointee-in-strange-places.rs:30:1
45+
|
46+
LL | #[pointee]
47+
| ^^^^^^^^^^
48+
49+
error: aborting due to 8 previous errors
50+

0 commit comments

Comments
 (0)