]> git.lizzy.rs Git - rust.git/blob - src/librustc/hir/check_attr.rs
Make inline associated constants a future compatibility warning
[rust.git] / src / librustc / hir / check_attr.rs
1 //! This module implements some validity checks for attributes.
2 //! In particular it verifies that `#[inline]` and `#[repr]` attributes are
3 //! attached to items that actually support them and if there are
4 //! conflicts between multiple such attributes attached to the same
5 //! item.
6
7 use crate::hir::{self, HirId, HirVec, Attribute, Item, ItemKind, TraitItem, TraitItemKind};
8 use crate::hir::DUMMY_HIR_ID;
9 use crate::hir::def_id::DefId;
10 use crate::hir::intravisit::{self, Visitor, NestedVisitorMap};
11 use crate::lint::builtin::UNUSED_ATTRIBUTES;
12 use crate::ty::TyCtxt;
13 use crate::ty::query::Providers;
14
15 use std::fmt::{self, Display};
16 use syntax::{attr, symbol::sym};
17 use syntax_pos::Span;
18
19 #[derive(Copy, Clone, PartialEq)]
20 pub(crate) enum MethodKind {
21     Trait { body: bool },
22     Inherent,
23 }
24
25 #[derive(Copy, Clone, PartialEq)]
26 pub(crate) enum Target {
27     ExternCrate,
28     Use,
29     Static,
30     Const,
31     Fn,
32     Closure,
33     Mod,
34     ForeignMod,
35     GlobalAsm,
36     TyAlias,
37     OpaqueTy,
38     Enum,
39     Struct,
40     Union,
41     Trait,
42     TraitAlias,
43     Impl,
44     Expression,
45     Statement,
46     AssocConst,
47     Method(MethodKind),
48     AssocTy,
49     ForeignFn,
50     ForeignStatic,
51     ForeignTy,
52 }
53
54 impl Display for Target {
55     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56         write!(f, "{}", match *self {
57             Target::ExternCrate => "extern crate",
58             Target::Use => "use",
59             Target::Static => "static item",
60             Target::Const => "constant item",
61             Target::Fn => "function",
62             Target::Closure => "closure",
63             Target::Mod => "module",
64             Target::ForeignMod => "foreign module",
65             Target::GlobalAsm => "global asm",
66             Target::TyAlias => "type alias",
67             Target::OpaqueTy => "opaque type",
68             Target::Enum => "enum",
69             Target::Struct => "struct",
70             Target::Union => "union",
71             Target::Trait => "trait",
72             Target::TraitAlias => "trait alias",
73             Target::Impl => "item",
74             Target::Expression => "expression",
75             Target::Statement => "statement",
76             Target::AssocConst => "associated const",
77             Target::Method(_) => "method",
78             Target::AssocTy => "associated type",
79             Target::ForeignFn => "foreign function",
80             Target::ForeignStatic => "foreign static item",
81             Target::ForeignTy => "foreign type",
82         })
83     }
84 }
85
86 impl Target {
87     pub(crate) fn from_item(item: &Item) -> Target {
88         match item.kind {
89             ItemKind::ExternCrate(..) => Target::ExternCrate,
90             ItemKind::Use(..) => Target::Use,
91             ItemKind::Static(..) => Target::Static,
92             ItemKind::Const(..) => Target::Const,
93             ItemKind::Fn(..) => Target::Fn,
94             ItemKind::Mod(..) => Target::Mod,
95             ItemKind::ForeignMod(..) => Target::ForeignMod,
96             ItemKind::GlobalAsm(..) => Target::GlobalAsm,
97             ItemKind::TyAlias(..) => Target::TyAlias,
98             ItemKind::OpaqueTy(..) => Target::OpaqueTy,
99             ItemKind::Enum(..) => Target::Enum,
100             ItemKind::Struct(..) => Target::Struct,
101             ItemKind::Union(..) => Target::Union,
102             ItemKind::Trait(..) => Target::Trait,
103             ItemKind::TraitAlias(..) => Target::TraitAlias,
104             ItemKind::Impl(..) => Target::Impl,
105         }
106     }
107
108     fn from_trait_item(trait_item: &TraitItem) -> Target {
109         match trait_item.kind {
110             TraitItemKind::Const(..) => Target::AssocConst,
111             TraitItemKind::Method(_, hir::TraitMethod::Required(_)) => {
112                 Target::Method(MethodKind::Trait { body: false })
113             }
114             TraitItemKind::Method(_, hir::TraitMethod::Provided(_)) => {
115                 Target::Method(MethodKind::Trait { body: true })
116             }
117             TraitItemKind::Type(..) => Target::AssocTy,
118         }
119     }
120
121     fn from_foreign_item(foreign_item: &hir::ForeignItem) -> Target {
122         match foreign_item.kind {
123             hir::ForeignItemKind::Fn(..) => Target::ForeignFn,
124             hir::ForeignItemKind::Static(..) => Target::ForeignStatic,
125             hir::ForeignItemKind::Type => Target::ForeignTy,
126         }
127     }
128
129     fn from_impl_item<'tcx>(tcx: TyCtxt<'tcx>, impl_item: &hir::ImplItem) -> Target {
130         match impl_item.kind {
131             hir::ImplItemKind::Const(..) => Target::AssocConst,
132             hir::ImplItemKind::Method(..) => {
133                 let parent_hir_id = tcx.hir().get_parent_item(impl_item.hir_id);
134                 let containing_item = tcx.hir().expect_item(parent_hir_id);
135                 let containing_impl_is_for_trait = match &containing_item.kind {
136                     hir::ItemKind::Impl(_, _, _, _, tr, _, _) => tr.is_some(),
137                     _ => bug!("parent of an ImplItem must be an Impl"),
138                 };
139                 if containing_impl_is_for_trait {
140                     Target::Method(MethodKind::Trait { body: true })
141                 } else {
142                     Target::Method(MethodKind::Inherent)
143                 }
144             }
145             hir::ImplItemKind::TyAlias(..) | hir::ImplItemKind::OpaqueTy(..) => Target::AssocTy,
146         }
147     }
148 }
149
150 struct CheckAttrVisitor<'tcx> {
151     tcx: TyCtxt<'tcx>,
152 }
153
154 impl CheckAttrVisitor<'tcx> {
155     /// Checks any attribute.
156     fn check_attributes(
157         &self,
158         hir_id: HirId,
159         attrs: &HirVec<Attribute>,
160         span: &Span,
161         target: Target,
162         item: Option<&Item>,
163     ) {
164         let mut is_valid = true;
165         for attr in attrs {
166             is_valid &= if attr.check_name(sym::inline) {
167                 self.check_inline(hir_id, attr, span, target)
168             } else if attr.check_name(sym::non_exhaustive) {
169                 self.check_non_exhaustive(attr, span, target)
170             } else if attr.check_name(sym::marker) {
171                 self.check_marker(attr, span, target)
172             } else if attr.check_name(sym::target_feature) {
173                 self.check_target_feature(attr, span, target)
174             } else if attr.check_name(sym::track_caller) {
175                 self.check_track_caller(&attr.span, attrs, span, target)
176             } else {
177                 true
178             };
179         }
180
181         if !is_valid {
182             return;
183         }
184
185         if target == Target::Fn {
186             self.tcx.codegen_fn_attrs(self.tcx.hir().local_def_id(hir_id));
187         }
188
189         self.check_repr(attrs, span, target, item);
190         self.check_used(attrs, target);
191     }
192
193     /// Checks if an `#[inline]` is applied to a function or a closure. Returns `true` if valid.
194     fn check_inline(&self, hir_id: HirId, attr: &Attribute, span: &Span, target: Target) -> bool {
195         match target {
196             Target::Fn | Target::Closure | Target::Method(MethodKind::Trait { body: true })
197             | Target::Method(MethodKind::Inherent) => true,
198             Target::Method(MethodKind::Trait { body: false }) | Target::ForeignFn => {
199                 self.tcx.struct_span_lint_hir(
200                     UNUSED_ATTRIBUTES,
201                     hir_id,
202                     attr.span,
203                     "`#[inline]` is ignored on function prototypes",
204                 ).emit();
205                 true
206             }
207             // FIXME(#65833): We permit associated consts to have an `#[inline]` attribute with
208             // just a lint, because we previously erroneously allowed it and some crates used it
209             // accidentally, to to be compatible with crates depending on them, we can't throw an
210             // error here.
211             Target::AssocConst => {
212                 self.tcx.struct_span_lint_hir(
213                     UNUSED_ATTRIBUTES,
214                     hir_id,
215                     attr.span,
216                     "`#[inline]` is ignored on constants",
217                 ).warn("this was previously accepted by the compiler but is \
218                        being phased out; it will become a hard error in \
219                        a future release!")
220                 .note("for more information, see issue #65833 \
221                        <https://github.com/rust-lang/rust/issues/65833>")
222                 .emit();
223                 true
224             }
225             _ => {
226                 struct_span_err!(
227                     self.tcx.sess,
228                     attr.span,
229                     E0518,
230                     "attribute should be applied to function or closure",
231                 ).span_label(*span, "not a function or closure")
232                     .emit();
233                 false
234             }
235         }
236     }
237
238     /// Checks if a `#[track_caller]` is applied to a non-naked function. Returns `true` if valid.
239     fn check_track_caller(
240         &self,
241         attr_span: &Span,
242         attrs: &HirVec<Attribute>,
243         span: &Span,
244         target: Target,
245     ) -> bool {
246         match target {
247             Target::Fn if attr::contains_name(attrs, sym::naked) => {
248                 struct_span_err!(
249                     self.tcx.sess,
250                     *attr_span,
251                     E0736,
252                     "cannot use `#[track_caller]` with `#[naked]`",
253                 ).emit();
254                 false
255             }
256             Target::Fn | Target::Method(MethodKind::Inherent) => true,
257             Target::Method(_) => {
258                 struct_span_err!(
259                     self.tcx.sess,
260                     *attr_span,
261                     E0738,
262                     "`#[track_caller]` may not be used on trait methods",
263                 ).emit();
264                 false
265             }
266             _ => {
267                 struct_span_err!(
268                     self.tcx.sess,
269                     *attr_span,
270                     E0739,
271                     "attribute should be applied to function"
272                 )
273                 .span_label(*span, "not a function")
274                 .emit();
275                 false
276             }
277         }
278     }
279
280     /// Checks if the `#[non_exhaustive]` attribute on an `item` is valid. Returns `true` if valid.
281     fn check_non_exhaustive(
282         &self,
283         attr: &Attribute,
284         span: &Span,
285         target: Target,
286     ) -> bool {
287         match target {
288             Target::Struct | Target::Enum => true,
289             _ => {
290                 struct_span_err!(self.tcx.sess,
291                                  attr.span,
292                                  E0701,
293                                  "attribute can only be applied to a struct or enum")
294                     .span_label(*span, "not a struct or enum")
295                     .emit();
296                 false
297             }
298         }
299     }
300
301     /// Checks if the `#[marker]` attribute on an `item` is valid. Returns `true` if valid.
302     fn check_marker(&self, attr: &Attribute, span: &Span, target: Target) -> bool {
303         match target {
304             Target::Trait => true,
305             _ => {
306                 self.tcx.sess
307                     .struct_span_err(attr.span, "attribute can only be applied to a trait")
308                     .span_label(*span, "not a trait")
309                     .emit();
310                 false
311             }
312         }
313     }
314
315     /// Checks if the `#[target_feature]` attribute on `item` is valid. Returns `true` if valid.
316     fn check_target_feature(&self, attr: &Attribute, span: &Span, target: Target) -> bool {
317         match target {
318             Target::Fn | Target::Method(MethodKind::Trait { body: true })
319             | Target::Method(MethodKind::Inherent) => true,
320             _ => {
321                 self.tcx.sess
322                     .struct_span_err(attr.span, "attribute should be applied to a function")
323                     .span_label(*span, "not a function")
324                     .emit();
325                 false
326             },
327         }
328     }
329
330     /// Checks if the `#[repr]` attributes on `item` are valid.
331     fn check_repr(
332         &self,
333         attrs: &HirVec<Attribute>,
334         span: &Span,
335         target: Target,
336         item: Option<&Item>,
337     ) {
338         // Extract the names of all repr hints, e.g., [foo, bar, align] for:
339         // ```
340         // #[repr(foo)]
341         // #[repr(bar, align(8))]
342         // ```
343         let hints: Vec<_> = attrs
344             .iter()
345             .filter(|attr| attr.check_name(sym::repr))
346             .filter_map(|attr| attr.meta_item_list())
347             .flatten()
348             .collect();
349
350         let mut int_reprs = 0;
351         let mut is_c = false;
352         let mut is_simd = false;
353         let mut is_transparent = false;
354
355         for hint in &hints {
356             let (article, allowed_targets) = match hint.name_or_empty() {
357                 name @ sym::C | name @ sym::align => {
358                     is_c |= name == sym::C;
359                     match target {
360                         Target::Struct | Target::Union | Target::Enum => continue,
361                         _ => ("a", "struct, enum, or union"),
362                     }
363                 }
364                 sym::packed => {
365                     if target != Target::Struct &&
366                             target != Target::Union {
367                                 ("a", "struct or union")
368                     } else {
369                         continue
370                     }
371                 }
372                 sym::simd => {
373                     is_simd = true;
374                     if target != Target::Struct {
375                         ("a", "struct")
376                     } else {
377                         continue
378                     }
379                 }
380                 sym::transparent => {
381                     is_transparent = true;
382                     match target {
383                         Target::Struct | Target::Union | Target::Enum => continue,
384                         _ => ("a", "struct, enum, or union"),
385                     }
386                 }
387                 sym::i8  | sym::u8  | sym::i16 | sym::u16 |
388                 sym::i32 | sym::u32 | sym::i64 | sym::u64 |
389                 sym::isize | sym::usize => {
390                     int_reprs += 1;
391                     if target != Target::Enum {
392                         ("an", "enum")
393                     } else {
394                         continue
395                     }
396                 }
397                 _ => continue,
398             };
399             self.emit_repr_error(
400                 hint.span(),
401                 *span,
402                 &format!("attribute should be applied to {}", allowed_targets),
403                 &format!("not {} {}", article, allowed_targets),
404             )
405         }
406
407         // Just point at all repr hints if there are any incompatibilities.
408         // This is not ideal, but tracking precisely which ones are at fault is a huge hassle.
409         let hint_spans = hints.iter().map(|hint| hint.span());
410
411         // Error on repr(transparent, <anything else>).
412         if is_transparent && hints.len() > 1 {
413             let hint_spans: Vec<_> = hint_spans.clone().collect();
414             span_err!(self.tcx.sess, hint_spans, E0692,
415                       "transparent {} cannot have other repr hints", target);
416         }
417         // Warn on repr(u8, u16), repr(C, simd), and c-like-enum-repr(C, u8)
418         if (int_reprs > 1)
419            || (is_simd && is_c)
420            || (int_reprs == 1 && is_c && item.map_or(false, |item| is_c_like_enum(item))) {
421             let hint_spans: Vec<_> = hint_spans.collect();
422             span_warn!(self.tcx.sess, hint_spans, E0566,
423                        "conflicting representation hints");
424         }
425     }
426
427     fn emit_repr_error(
428         &self,
429         hint_span: Span,
430         label_span: Span,
431         hint_message: &str,
432         label_message: &str,
433     ) {
434         struct_span_err!(self.tcx.sess, hint_span, E0517, "{}", hint_message)
435             .span_label(label_span, label_message)
436             .emit();
437     }
438
439     fn check_stmt_attributes(&self, stmt: &hir::Stmt) {
440         // When checking statements ignore expressions, they will be checked later
441         if let hir::StmtKind::Local(ref l) = stmt.kind {
442             for attr in l.attrs.iter() {
443                 if attr.check_name(sym::inline) {
444                     self.check_inline(DUMMY_HIR_ID, attr, &stmt.span, Target::Statement);
445                 }
446                 if attr.check_name(sym::repr) {
447                     self.emit_repr_error(
448                         attr.span,
449                         stmt.span,
450                         "attribute should not be applied to a statement",
451                         "not a struct, enum, or union",
452                     );
453                 }
454             }
455         }
456     }
457
458     fn check_expr_attributes(&self, expr: &hir::Expr) {
459         let target = match expr.kind {
460             hir::ExprKind::Closure(..) => Target::Closure,
461             _ => Target::Expression,
462         };
463         for attr in expr.attrs.iter() {
464             if attr.check_name(sym::inline) {
465                 self.check_inline(DUMMY_HIR_ID, attr, &expr.span, target);
466             }
467             if attr.check_name(sym::repr) {
468                 self.emit_repr_error(
469                     attr.span,
470                     expr.span,
471                     "attribute should not be applied to an expression",
472                     "not defining a struct, enum, or union",
473                 );
474             }
475         }
476     }
477
478     fn check_used(&self, attrs: &HirVec<Attribute>, target: Target) {
479         for attr in attrs {
480             if attr.check_name(sym::used) && target != Target::Static {
481                 self.tcx.sess
482                     .span_err(attr.span, "attribute must be applied to a `static` variable");
483             }
484         }
485     }
486 }
487
488 impl Visitor<'tcx> for CheckAttrVisitor<'tcx> {
489     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
490         NestedVisitorMap::OnlyBodies(&self.tcx.hir())
491     }
492
493     fn visit_item(&mut self, item: &'tcx Item) {
494         let target = Target::from_item(item);
495         self.check_attributes(item.hir_id, &item.attrs, &item.span, target, Some(item));
496         intravisit::walk_item(self, item)
497     }
498
499     fn visit_trait_item(&mut self, trait_item: &'tcx TraitItem) {
500         let target = Target::from_trait_item(trait_item);
501         self.check_attributes(trait_item.hir_id, &trait_item.attrs, &trait_item.span, target, None);
502         intravisit::walk_trait_item(self, trait_item)
503     }
504
505     fn visit_foreign_item(&mut self, f_item: &'tcx hir::ForeignItem) {
506         let target = Target::from_foreign_item(f_item);
507         self.check_attributes(f_item.hir_id, &f_item.attrs, &f_item.span, target, None);
508         intravisit::walk_foreign_item(self, f_item)
509     }
510
511     fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem) {
512         let target = Target::from_impl_item(self.tcx, impl_item);
513         self.check_attributes(impl_item.hir_id, &impl_item.attrs, &impl_item.span, target, None);
514         intravisit::walk_impl_item(self, impl_item)
515     }
516
517     fn visit_stmt(&mut self, stmt: &'tcx hir::Stmt) {
518         self.check_stmt_attributes(stmt);
519         intravisit::walk_stmt(self, stmt)
520     }
521
522     fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
523         self.check_expr_attributes(expr);
524         intravisit::walk_expr(self, expr)
525     }
526 }
527
528 fn is_c_like_enum(item: &Item) -> bool {
529     if let ItemKind::Enum(ref def, _) = item.kind {
530         for variant in &def.variants {
531             match variant.data {
532                 hir::VariantData::Unit(..) => { /* continue */ }
533                 _ => return false,
534             }
535         }
536         true
537     } else {
538         false
539     }
540 }
541
542 fn check_mod_attrs(tcx: TyCtxt<'_>, module_def_id: DefId) {
543     tcx.hir().visit_item_likes_in_module(
544         module_def_id,
545         &mut CheckAttrVisitor { tcx }.as_deep_visitor()
546     );
547 }
548
549 pub(crate) fn provide(providers: &mut Providers<'_>) {
550     *providers = Providers {
551         check_mod_attrs,
552         ..*providers
553     };
554 }