Skip to content

Commit ac6d6d9

Browse files
committed
Make error code registration work again. #19624
1 parent 378fb58 commit ac6d6d9

File tree

11 files changed

+128
-80
lines changed

11 files changed

+128
-80
lines changed

mk/tests.mk

+1
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ tidy:
302302
| grep '^$(S)src/libbacktrace' -v \
303303
| grep '^$(S)src/rust-installer' -v \
304304
| xargs $(CFG_PYTHON) $(S)src/etc/check-binaries.py
305+
$(Q) $(CFG_PYTHON) $(S)src/etc/errorck.py $(S)src/
305306

306307

307308
endif

src/etc/errorck.py

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Copyright 2015 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+
# Digs error codes out of files named 'diagnostics.rs' across
12+
# the tree, and ensures thare are no duplicates.
13+
14+
import sys, os, re
15+
16+
src_dir = sys.argv[1]
17+
18+
errcode_map = { }
19+
20+
for (dirpath, dirnames, filenames) in os.walk(src_dir):
21+
22+
if "src/test" in dirpath or "src/llvm" in dirpath:
23+
# Short circuit for fast
24+
continue
25+
26+
for filename in filenames:
27+
if filename != "diagnostics.rs":
28+
continue
29+
30+
path = os.path.join(dirpath, filename)
31+
line_num = 1
32+
with open(path, 'r') as f:
33+
for line in f:
34+
35+
p = re.compile("(E\d\d\d\d)")
36+
m = p.search(line)
37+
if not m is None:
38+
errcode = m.group(1)
39+
40+
new_record = [(errcode, path, line_num, line)]
41+
existing = errcode_map.get(errcode)
42+
if existing is not None:
43+
# This is a dupe
44+
errcode_map[errcode] = existing + new_record
45+
else:
46+
errcode_map[errcode] = new_record
47+
48+
line_num += 1
49+
50+
errors = False
51+
all_errors = []
52+
for errcode in errcode_map:
53+
entries = errcode_map[errcode]
54+
all_errors += [entries[0][0]]
55+
if len(entries) > 1:
56+
print "error: duplicate error code " + errcode
57+
for entry in entries:
58+
print entry[1] + ": " + str(entry[2])
59+
print entry[3]
60+
errors = True
61+
62+
print str(len(errcode_map)) + " error codes"
63+
64+
all_errors.sort()
65+
all_errors.reverse()
66+
67+
print "highest error code: " + all_errors[0]
68+
69+
if errors:
70+
sys.exit(1)

src/librustc/diagnostics.rs

+4-16
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ register_diagnostics! {
3131
E0010,
3232
E0011,
3333
E0012,
34-
E0013,
3534
E0014,
3635
E0015,
3736
E0016,
@@ -49,24 +48,13 @@ register_diagnostics! {
4948
E0137,
5049
E0138,
5150
E0139,
52-
E0140,
5351
E0152,
54-
E0153,
55-
E0157,
5652
E0158,
5753
E0161,
5854
E0162,
5955
E0165,
60-
E0166,
61-
E0167,
62-
E0168,
63-
E0169,
64-
E0170,
65-
E0171,
66-
E0172,
67-
E0173,
68-
E0174,
69-
E0177,
70-
E0178,
71-
E0179
56+
E0170
7257
}
58+
59+
__build_diagnostic_array! { DIAGNOSTICS }
60+

src/librustc/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ extern crate test;
5252

5353
pub use rustc_llvm as llvm;
5454

55-
mod diagnostics;
55+
// NB: This module needs to be declared first so diagnostics are
56+
// registered before they are used.
57+
pub mod diagnostics;
5658

5759
pub mod back {
5860
pub use rustc_back::abi;
@@ -130,8 +132,6 @@ pub mod lib {
130132
pub use llvm;
131133
}
132134

133-
__build_diagnostic_array! { DIAGNOSTICS }
134-
135135
// A private module so that macro-expanded idents like
136136
// `::rustc::lint::Lint` will also work in `rustc` itself.
137137
//

src/librustc_driver/lib.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ use rustc::lint::Lint;
5959
use rustc::lint;
6060
use rustc::metadata;
6161
use rustc::metadata::creader::CrateOrString::Str;
62-
use rustc::DIAGNOSTICS;
6362
use rustc::util::common::time;
6463

6564
use std::cmp::Ordering::Equal;
@@ -96,7 +95,7 @@ fn run_compiler(args: &[String]) {
9695
None => return
9796
};
9897

99-
let descriptions = diagnostics::registry::Registry::new(&DIAGNOSTICS);
98+
let descriptions = diagnostics_registry();
10099
match matches.opt_str("explain") {
101100
Some(ref code) => {
102101
match descriptions.find_description(&code[]) {
@@ -657,8 +656,20 @@ pub fn monitor<F:FnOnce()+Send>(f: F) {
657656
}
658657
}
659658

659+
pub fn diagnostics_registry() -> diagnostics::registry::Registry {
660+
use syntax::diagnostics::registry::Registry;
661+
662+
let all_errors = Vec::new() +
663+
rustc::diagnostics::DIAGNOSTICS.as_slice() +
664+
rustc_typeck::diagnostics::DIAGNOSTICS.as_slice() +
665+
rustc_resolve::diagnostics::DIAGNOSTICS.as_slice();
666+
667+
Registry::new(&*all_errors)
668+
}
669+
660670
pub fn main() {
661671
let args = std::os::args();
662672
let result = run(args);
663673
std::os::set_exit_status(result);
664674
}
675+

src/librustc_resolve/diagnostics.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
register_diagnostics! {
14+
E0157,
15+
E0153
16+
}
17+
18+
__build_diagnostic_array! { DIAGNOSTICS }

src/librustc_resolve/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ use std::mem::replace;
9595
use std::rc::{Rc, Weak};
9696
use std::uint;
9797

98+
// NB: This module needs to be declared first so diagnostics are
99+
// registered before they are used.
100+
pub mod diagnostics;
101+
98102
mod check_unused;
99103
mod record_exports;
100104
mod build_reduced_graph;

src/librustc_typeck/diagnostics.rs

+3-58
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,7 @@
1010

1111
#![allow(non_snake_case)]
1212

13-
register_diagnostic! {
14-
E0001,
15-
r##"
16-
This error suggests that the expression arm corresponding to the noted pattern
17-
will never be reached as for all possible values of the expression being matched,
18-
one of the preceeding patterns will match.
19-
20-
This means that perhaps some of the preceeding patterns are too general, this
21-
one is too specific or the ordering is incorrect.
22-
"## }
23-
2413
register_diagnostics! {
25-
E0002,
26-
E0003,
27-
E0004,
28-
E0005,
29-
E0006,
30-
E0007,
31-
E0008,
32-
E0009,
33-
E0010,
34-
E0011,
35-
E0012,
36-
E0013,
37-
E0014,
38-
E0015,
39-
E0016,
40-
E0017,
41-
E0018,
42-
E0019,
43-
E0020,
44-
E0022,
4514
E0023,
4615
E0024,
4716
E0025,
@@ -61,12 +30,9 @@ register_diagnostics! {
6130
E0046,
6231
E0049,
6332
E0050,
64-
E0051,
65-
E0052,
6633
E0053,
6734
E0054,
6835
E0055,
69-
E0056,
7036
E0057,
7137
E0059,
7238
E0060,
@@ -101,16 +67,12 @@ register_diagnostics! {
10167
E0092,
10268
E0093,
10369
E0094,
104-
E0100,
10570
E0101,
10671
E0102,
10772
E0103,
10873
E0104,
10974
E0106,
11075
E0107,
111-
E0108,
112-
E0109,
113-
E0110,
11476
E0116,
11577
E0117,
11678
E0118,
@@ -125,38 +87,21 @@ register_diagnostics! {
12587
E0130,
12688
E0131,
12789
E0132,
128-
E0133,
129-
E0134,
130-
E0135,
131-
E0136,
132-
E0137,
133-
E0138,
134-
E0139,
135-
E0140,
13690
E0141,
137-
E0152,
138-
E0153,
139-
E0157,
140-
E0158,
14191
E0159,
142-
E0161,
143-
E0162,
14492
E0163,
14593
E0164,
146-
E0165,
14794
E0166,
14895
E0167,
14996
E0168,
150-
E0169,
151-
E0171,
15297
E0172,
15398
E0173, // manual implementations of unboxed closure traits are experimental
15499
E0174, // explicit use of unboxed closure methods are experimental
155-
E0177,
156100
E0178,
157-
E0180,
158-
E0181,
159101
E0182,
160102
E0183,
161103
E0184
162104
}
105+
106+
__build_diagnostic_array! { DIAGNOSTICS }
107+

src/librustc_typeck/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ use syntax::ast_util::local_def;
110110

111111
use std::cell::RefCell;
112112

113+
// NB: This module needs to be declared first so diagnostics are
114+
// registered before they are used.
115+
pub mod diagnostics;
116+
113117
mod check;
114118
mod rscope;
115119
mod astconv;

src/libsyntax/diagnostics/plugin.rs

+7
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ pub fn expand_diagnostic_used<'cx>(ecx: &'cx mut ExtCtxt,
6565
}
6666
()
6767
});
68+
with_registered_diagnostics(|diagnostics| {
69+
if !diagnostics.contains_key(&code.name) {
70+
ecx.span_err(span, &format!(
71+
"used diagnostic code {} not registered", token::get_ident(code).get()
72+
)[]);
73+
}
74+
});
6875
MacExpr::new(quote_expr!(ecx, ()))
6976
}
7077

src/test/run-make/issue-19371/foo.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ fn basic_sess(sysroot: Path) -> Session {
4444
opts.output_types = vec![OutputTypeExe];
4545
opts.maybe_sysroot = Some(sysroot);
4646

47-
let descriptions = Registry::new(&rustc::DIAGNOSTICS);
47+
let descriptions = Registry::new(&rustc::diagnostics::DIAGNOSTICS);
4848
let sess = build_session(opts, None, descriptions);
4949
sess
5050
}

0 commit comments

Comments
 (0)