Skip to content

Commit 0e326d4

Browse files
Add error codes in libsyntax
1 parent ea0dc92 commit 0e326d4

File tree

4 files changed

+142
-55
lines changed

4 files changed

+142
-55
lines changed

src/libsyntax/attr.rs

+65-40
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,27 @@ thread_local! {
3434
static USED_ATTRS: RefCell<Vec<u64>> = RefCell::new(Vec::new())
3535
}
3636

37+
enum AttrError {
38+
MultipleItem(InternedString),
39+
UnknownMetaItem(InternedString),
40+
MissingSince,
41+
MissingFeature,
42+
MultipleStabilityLevels,
43+
}
44+
45+
fn handle_errors(diag: &Handler, span: Span, error: AttrError) {
46+
match error {
47+
AttrError::MultipleItem(item) => span_err!(diag, span, E0538,
48+
"multiple '{}' items", item),
49+
AttrError::UnknownMetaItem(item) => span_err!(diag, span, E0541,
50+
"unknown meta item '{}'", item),
51+
AttrError::MissingSince => span_err!(diag, span, E0542, "missing 'since'"),
52+
AttrError::MissingFeature => span_err!(diag, span, E0546, "missing 'feature'"),
53+
AttrError::MultipleStabilityLevels => span_err!(diag, span, E0544,
54+
"multiple stability levels"),
55+
}
56+
}
57+
3758
pub fn mark_used(attr: &Attribute) {
3859
let AttrId(id) = attr.node.id;
3960
USED_ATTRS.with(|slot| {
@@ -303,10 +324,10 @@ pub fn find_export_name_attr(diag: &Handler, attrs: &[Attribute]) -> Option<Inte
303324
if let s@Some(_) = attr.value_str() {
304325
s
305326
} else {
306-
diag.struct_span_err(attr.span,
307-
"export_name attribute has invalid format")
308-
.help("use #[export_name=\"*\"]")
309-
.emit();
327+
struct_span_err!(diag, attr.span, E0533,
328+
"export_name attribute has invalid format")
329+
.help("use #[export_name=\"*\"]")
330+
.emit();
310331
None
311332
}
312333
} else {
@@ -339,14 +360,16 @@ pub fn find_inline_attr(diagnostic: Option<&Handler>, attrs: &[Attribute]) -> In
339360
MetaItemKind::List(ref n, ref items) if n == "inline" => {
340361
mark_used(attr);
341362
if items.len() != 1 {
342-
diagnostic.map(|d|{ d.span_err(attr.span, "expected one argument"); });
363+
diagnostic.map(|d|{ span_err!(d, attr.span, E0534, "expected one argument"); });
343364
InlineAttr::None
344365
} else if contains_name(&items[..], "always") {
345366
InlineAttr::Always
346367
} else if contains_name(&items[..], "never") {
347368
InlineAttr::Never
348369
} else {
349-
diagnostic.map(|d|{ d.span_err((*items[0]).span, "invalid argument"); });
370+
diagnostic.map(|d| {
371+
span_err!(d, (*items[0]).span, E0535, "invalid argument");
372+
});
350373
InlineAttr::None
351374
}
352375
}
@@ -374,13 +397,13 @@ pub fn cfg_matches(cfgs: &[P<MetaItem>], cfg: &ast::MetaItem,
374397
mis.iter().all(|mi| cfg_matches(cfgs, &mi, sess, features)),
375398
ast::MetaItemKind::List(ref pred, ref mis) if &pred[..] == "not" => {
376399
if mis.len() != 1 {
377-
sess.span_diagnostic.span_err(cfg.span, "expected 1 cfg-pattern");
400+
span_err!(sess.span_diagnostic, cfg.span, E0536, "expected 1 cfg-pattern");
378401
return false;
379402
}
380403
!cfg_matches(cfgs, &mis[0], sess, features)
381404
}
382405
ast::MetaItemKind::List(ref pred, _) => {
383-
sess.span_diagnostic.span_err(cfg.span, &format!("invalid predicate `{}`", pred));
406+
span_err!(sess.span_diagnostic, cfg.span, E0537, "invalid predicate `{}`", pred);
384407
false
385408
},
386409
ast::MetaItemKind::Word(_) | ast::MetaItemKind::NameValue(..) => {
@@ -446,23 +469,23 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
446469
if let Some(metas) = attr.meta_item_list() {
447470
let get = |meta: &MetaItem, item: &mut Option<InternedString>| {
448471
if item.is_some() {
449-
diagnostic.span_err(meta.span, &format!("multiple '{}' items",
450-
meta.name()));
472+
handle_errors(diagnostic, meta.span, AttrError::MultipleItem(meta.name()));
451473
return false
452474
}
453475
if let Some(v) = meta.value_str() {
454476
*item = Some(v);
455477
true
456478
} else {
457-
diagnostic.span_err(meta.span, "incorrect meta item");
479+
span_err!(diagnostic, meta.span, E0539, "incorrect meta item");
458480
false
459481
}
460482
};
461483

462484
match tag {
463485
"rustc_deprecated" => {
464486
if rustc_depr.is_some() {
465-
diagnostic.span_err(item_sp, "multiple rustc_deprecated attributes");
487+
span_err!(diagnostic, item_sp, E0540,
488+
"multiple rustc_deprecated attributes");
466489
break
467490
}
468491

@@ -473,8 +496,8 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
473496
"since" => if !get(meta, &mut since) { continue 'outer },
474497
"reason" => if !get(meta, &mut reason) { continue 'outer },
475498
_ => {
476-
diagnostic.span_err(meta.span, &format!("unknown meta item '{}'",
477-
meta.name()));
499+
handle_errors(diagnostic, meta.span,
500+
AttrError::UnknownMetaItem(meta.name()));
478501
continue 'outer
479502
}
480503
}
@@ -488,18 +511,18 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
488511
})
489512
}
490513
(None, _) => {
491-
diagnostic.span_err(attr.span(), "missing 'since'");
514+
handle_errors(diagnostic, attr.span(), AttrError::MissingSince);
492515
continue
493516
}
494517
_ => {
495-
diagnostic.span_err(attr.span(), "missing 'reason'");
518+
span_err!(diagnostic, attr.span(), E0543, "missing 'reason'");
496519
continue
497520
}
498521
}
499522
}
500523
"unstable" => {
501524
if stab.is_some() {
502-
diagnostic.span_err(item_sp, "multiple stability levels");
525+
handle_errors(diagnostic, attr.span(), AttrError::MultipleStabilityLevels);
503526
break
504527
}
505528

@@ -512,8 +535,8 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
512535
"reason" => if !get(meta, &mut reason) { continue 'outer },
513536
"issue" => if !get(meta, &mut issue) { continue 'outer },
514537
_ => {
515-
diagnostic.span_err(meta.span, &format!("unknown meta item '{}'",
516-
meta.name()));
538+
handle_errors(diagnostic, meta.span,
539+
AttrError::UnknownMetaItem(meta.name()));
517540
continue 'outer
518541
}
519542
}
@@ -528,7 +551,8 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
528551
if let Ok(issue) = issue.parse() {
529552
issue
530553
} else {
531-
diagnostic.span_err(attr.span(), "incorrect 'issue'");
554+
span_err!(diagnostic, attr.span(), E0545,
555+
"incorrect 'issue'");
532556
continue
533557
}
534558
}
@@ -538,18 +562,18 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
538562
})
539563
}
540564
(None, _, _) => {
541-
diagnostic.span_err(attr.span(), "missing 'feature'");
565+
handle_errors(diagnostic, attr.span(), AttrError::MissingFeature);
542566
continue
543567
}
544568
_ => {
545-
diagnostic.span_err(attr.span(), "missing 'issue'");
569+
span_err!(diagnostic, attr.span(), E0547, "missing 'issue'");
546570
continue
547571
}
548572
}
549573
}
550574
"stable" => {
551575
if stab.is_some() {
552-
diagnostic.span_err(item_sp, "multiple stability levels");
576+
handle_errors(diagnostic, attr.span(), AttrError::MultipleStabilityLevels);
553577
break
554578
}
555579

@@ -560,8 +584,8 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
560584
"feature" => if !get(meta, &mut feature) { continue 'outer },
561585
"since" => if !get(meta, &mut since) { continue 'outer },
562586
_ => {
563-
diagnostic.span_err(meta.span, &format!("unknown meta item '{}'",
564-
meta.name()));
587+
handle_errors(diagnostic, meta.span,
588+
AttrError::UnknownMetaItem(meta.name()));
565589
continue 'outer
566590
}
567591
}
@@ -578,19 +602,19 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
578602
})
579603
}
580604
(None, _) => {
581-
diagnostic.span_err(attr.span(), "missing 'feature'");
605+
handle_errors(diagnostic, attr.span(), AttrError::MissingFeature);
582606
continue
583607
}
584608
_ => {
585-
diagnostic.span_err(attr.span(), "missing 'since'");
609+
handle_errors(diagnostic, attr.span(), AttrError::MissingSince);
586610
continue
587611
}
588612
}
589613
}
590614
_ => unreachable!()
591615
}
592616
} else {
593-
diagnostic.span_err(attr.span(), "incorrect stability attribute type");
617+
span_err!(diagnostic, attr.span(), E0548, "incorrect stability attribute type");
594618
continue
595619
}
596620
}
@@ -603,8 +627,9 @@ fn find_stability_generic<'a, I>(diagnostic: &Handler,
603627
}
604628
stab.rustc_depr = Some(rustc_depr);
605629
} else {
606-
diagnostic.span_err(item_sp, "rustc_deprecated attribute must be paired with \
607-
either stable or unstable attribute");
630+
span_err!(diagnostic, item_sp, E0549,
631+
"rustc_deprecated attribute must be paired with \
632+
either stable or unstable attribute");
608633
}
609634
}
610635

@@ -627,22 +652,21 @@ fn find_deprecation_generic<'a, I>(diagnostic: &Handler,
627652
mark_used(attr);
628653

629654
if depr.is_some() {
630-
diagnostic.span_err(item_sp, "multiple deprecated attributes");
655+
span_err!(diagnostic, item_sp, E0550, "multiple deprecated attributes");
631656
break
632657
}
633658

634659
depr = if let Some(metas) = attr.meta_item_list() {
635660
let get = |meta: &MetaItem, item: &mut Option<InternedString>| {
636661
if item.is_some() {
637-
diagnostic.span_err(meta.span, &format!("multiple '{}' items",
638-
meta.name()));
662+
handle_errors(diagnostic, meta.span, AttrError::MultipleItem(meta.name()));
639663
return false
640664
}
641665
if let Some(v) = meta.value_str() {
642666
*item = Some(v);
643667
true
644668
} else {
645-
diagnostic.span_err(meta.span, "incorrect meta item");
669+
span_err!(diagnostic, meta.span, E0551, "incorrect meta item");
646670
false
647671
}
648672
};
@@ -654,8 +678,8 @@ fn find_deprecation_generic<'a, I>(diagnostic: &Handler,
654678
"since" => if !get(meta, &mut since) { continue 'outer },
655679
"note" => if !get(meta, &mut note) { continue 'outer },
656680
_ => {
657-
diagnostic.span_err(meta.span, &format!("unknown meta item '{}'",
658-
meta.name()));
681+
handle_errors(diagnostic, meta.span,
682+
AttrError::UnknownMetaItem(meta.name()));
659683
continue 'outer
660684
}
661685
}
@@ -689,7 +713,7 @@ pub fn require_unique_names(diagnostic: &Handler, metas: &[P<MetaItem>]) {
689713

690714
if !set.insert(name.clone()) {
691715
panic!(diagnostic.span_fatal(meta.span,
692-
&format!("duplicate meta item `{}`", name)));
716+
&format!("duplicate meta item `{}`", name)));
693717
}
694718
}
695719
}
@@ -718,8 +742,8 @@ pub fn find_repr_attrs(diagnostic: &Handler, attr: &Attribute) -> Vec<ReprAttr>
718742
Some(ity) => Some(ReprInt(item.span, ity)),
719743
None => {
720744
// Not a word we recognize
721-
diagnostic.span_err(item.span,
722-
"unrecognized representation hint");
745+
span_err!(diagnostic, item.span, E0552,
746+
"unrecognized representation hint");
723747
None
724748
}
725749
}
@@ -731,7 +755,8 @@ pub fn find_repr_attrs(diagnostic: &Handler, attr: &Attribute) -> Vec<ReprAttr>
731755
}
732756
}
733757
// Not a word:
734-
_ => diagnostic.span_err(item.span, "unrecognized enum representation hint")
758+
_ => span_err!(diagnostic, item.span, E0553,
759+
"unrecognized enum representation hint"),
735760
}
736761
}
737762
}

src/libsyntax/diagnostic_list.rs

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![allow(non_snake_case)]
12+
13+
// Error messages for EXXXX errors.
14+
// Each message should start and end with a new line, and be wrapped to 80 characters.
15+
// In vim you can `:set tw=80` and use `gq` to wrap paragraphs. Use `:set tw=0` to disable.
16+
register_long_diagnostics! {
17+
18+
E0533: r##"
19+
```compile_fail,E0533
20+
#[export_name]
21+
pub fn something() {}
22+
```
23+
"##,
24+
25+
}
26+
27+
28+
register_diagnostics! {
29+
E0534,
30+
E0535,
31+
E0536,
32+
E0537,
33+
E0538,
34+
E0539,
35+
E0540,
36+
E0541,
37+
E0542,
38+
E0543,
39+
E0544,
40+
E0545,
41+
E0546,
42+
E0547,
43+
E0548,
44+
E0549,
45+
E0550,
46+
E0551,
47+
E0552,
48+
E0553,
49+
E0554,
50+
E0555,
51+
E0556,
52+
E0557,
53+
E0558,
54+
}

src/libsyntax/feature_gate.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -1103,17 +1103,16 @@ pub fn get_features(span_handler: &Handler, krate_attrs: &[ast::Attribute]) -> F
11031103

11041104
match attr.meta_item_list() {
11051105
None => {
1106-
span_handler.span_err(attr.span, "malformed feature attribute, \
1107-
expected #![feature(...)]");
1106+
span_err!(span_handler, attr.span, E0555,
1107+
"malformed feature attribute, expected #![feature(...)]");
11081108
}
11091109
Some(list) => {
11101110
for mi in list {
11111111
let name = match mi.node {
11121112
ast::MetaItemKind::Word(ref word) => (*word).clone(),
11131113
_ => {
1114-
span_handler.span_err(mi.span,
1115-
"malformed feature, expected just \
1116-
one word");
1114+
span_err!(span_handler, mi.span, E0556,
1115+
"malformed feature, expected just one word");
11171116
continue
11181117
}
11191118
};
@@ -1123,7 +1122,7 @@ pub fn get_features(span_handler: &Handler, krate_attrs: &[ast::Attribute]) -> F
11231122
}
11241123
else if let Some(&(_, _, _)) = REMOVED_FEATURES.iter()
11251124
.find(|& &(n, _, _)| name == n) {
1126-
span_handler.span_err(mi.span, "feature has been removed");
1125+
span_err!(span_handler, mi.span, E0557, "feature has been removed");
11271126
}
11281127
else if let Some(&(_, _, _)) = ACCEPTED_FEATURES.iter()
11291128
.find(|& &(n, _, _)| name == n) {
@@ -1179,9 +1178,9 @@ fn maybe_stage_features(span_handler: &Handler, krate: &ast::Crate,
11791178
for attr in &krate.attrs {
11801179
if attr.check_name("feature") {
11811180
let release_channel = option_env!("CFG_RELEASE_CHANNEL").unwrap_or("(unknown)");
1182-
let ref msg = format!("#[feature] may not be used on the {} release channel",
1183-
release_channel);
1184-
span_handler.span_err(attr.span, msg);
1181+
span_err!(span_handler, attr.span, E0558,
1182+
"#[feature] may not be used on the {} release channel",
1183+
release_channel);
11851184
}
11861185
}
11871186
}

0 commit comments

Comments
 (0)