Skip to content

Commit a5fe176

Browse files
author
Jakub Wieczorek
committed
Convert a first batch of diagnostics to have error codes
1 parent 350f3aa commit a5fe176

File tree

11 files changed

+410
-390
lines changed

11 files changed

+410
-390
lines changed

src/librustc/diagnostics.rs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,99 @@ register_diagnostic!(E0001, r##"
1616
This means that perhaps some of the preceeding patterns are too general, this
1717
one is too specific or the ordering is incorrect.
1818
"##)
19+
20+
register_diagnostics!(
21+
E0002,
22+
E0003,
23+
E0004,
24+
E0005,
25+
E0006,
26+
E0007,
27+
E0008,
28+
E0009,
29+
E0010,
30+
E0011,
31+
E0012,
32+
E0013,
33+
E0014,
34+
E0015,
35+
E0016,
36+
E0017,
37+
E0018,
38+
E0019,
39+
E0020,
40+
E0021,
41+
E0022,
42+
E0023,
43+
E0024,
44+
E0025,
45+
E0026,
46+
E0027,
47+
E0028,
48+
E0029,
49+
E0030,
50+
E0031,
51+
E0032,
52+
E0033,
53+
E0034,
54+
E0035,
55+
E0036,
56+
E0037,
57+
E0038,
58+
E0039,
59+
E0040,
60+
E0041,
61+
E0042,
62+
E0043,
63+
E0044,
64+
E0045,
65+
E0046,
66+
E0047,
67+
E0048,
68+
E0049,
69+
E0050,
70+
E0051,
71+
E0052,
72+
E0053,
73+
E0054,
74+
E0055,
75+
E0056,
76+
E0057,
77+
E0058,
78+
E0059,
79+
E0060,
80+
E0061,
81+
E0062,
82+
E0063,
83+
E0064,
84+
E0065,
85+
E0066,
86+
E0067,
87+
E0068,
88+
E0069,
89+
E0070,
90+
E0071,
91+
E0072,
92+
E0073,
93+
E0074,
94+
E0075,
95+
E0076,
96+
E0077,
97+
E0078,
98+
E0079,
99+
E0080,
100+
E0081,
101+
E0082,
102+
E0083,
103+
E0084,
104+
E0085,
105+
E0086,
106+
E0087,
107+
E0088,
108+
E0089,
109+
E0090,
110+
E0091,
111+
E0092,
112+
E0093,
113+
E0094
114+
)

src/librustc/driver/session.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ impl Session {
8585
pub fn span_warn(&self, sp: Span, msg: &str) {
8686
self.diagnostic().span_warn(sp, msg)
8787
}
88+
pub fn span_warn_with_code(&self, sp: Span, msg: &str, code: &str) {
89+
self.diagnostic().span_warn_with_code(sp, msg, code)
90+
}
8891
pub fn warn(&self, msg: &str) {
8992
self.diagnostic().handler().warn(msg)
9093
}

src/librustc/middle/check_const.rs

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -87,28 +87,25 @@ fn check_expr(v: &mut CheckCrateVisitor, e: &Expr, is_const: bool) {
8787
match e.node {
8888
ExprUnary(UnDeref, _) => { }
8989
ExprUnary(UnBox, _) | ExprUnary(UnUniq, _) => {
90-
v.tcx.sess.span_err(e.span,
91-
"cannot do allocations in constant expressions");
90+
span_err!(v.tcx.sess, e.span, E0010, "cannot do allocations in constant expressions");
9291
return;
9392
}
9493
ExprLit(lit) if ast_util::lit_is_str(lit) => {}
9594
ExprBinary(..) | ExprUnary(..) => {
9695
let method_call = typeck::MethodCall::expr(e.id);
9796
if v.tcx.method_map.borrow().contains_key(&method_call) {
98-
v.tcx.sess.span_err(e.span, "user-defined operators are not \
99-
allowed in constant expressions");
97+
span_err!(v.tcx.sess, e.span, E0011,
98+
"user-defined operators are not allowed in constant expressions");
10099
}
101100
}
102101
ExprLit(_) => (),
103102
ExprCast(_, _) => {
104103
let ety = ty::expr_ty(v.tcx, e);
105104
if !ty::type_is_numeric(ety) && !ty::type_is_unsafe_ptr(ety) {
106-
v.tcx
107-
.sess
108-
.span_err(e.span,
109-
format!("can not cast to `{}` in a constant \
110-
expression",
111-
ppaux::ty_to_string(v.tcx, ety)).as_slice())
105+
span_err!(v.tcx.sess, e.span, E0012,
106+
"can not cast to `{}` in a constant expression",
107+
ppaux::ty_to_string(v.tcx, ety)
108+
);
112109
}
113110
}
114111
ExprPath(ref pth) => {
@@ -117,9 +114,8 @@ fn check_expr(v: &mut CheckCrateVisitor, e: &Expr, is_const: bool) {
117114
// foo::<bar> in a const. Currently that is only done on
118115
// a path in trans::callee that only works in block contexts.
119116
if !pth.segments.iter().all(|segment| segment.types.is_empty()) {
120-
v.tcx.sess.span_err(e.span,
121-
"paths in constants may only refer to \
122-
items without type parameters");
117+
span_err!(v.tcx.sess, e.span, E0013,
118+
"paths in constants may only refer to items without type parameters");
123119
}
124120
match v.tcx.def_map.borrow().find(&e.id) {
125121
Some(&DefStatic(..)) |
@@ -129,9 +125,8 @@ fn check_expr(v: &mut CheckCrateVisitor, e: &Expr, is_const: bool) {
129125

130126
Some(&def) => {
131127
debug!("(checking const) found bad def: {:?}", def);
132-
v.tcx.sess.span_err(e.span,
133-
"paths in constants may only refer to \
134-
constants or functions");
128+
span_err!(v.tcx.sess, e.span, E0014,
129+
"paths in constants may only refer to constants or functions");
135130
}
136131
None => {
137132
v.tcx.sess.span_bug(e.span, "unbound path in const?!");
@@ -143,19 +138,17 @@ fn check_expr(v: &mut CheckCrateVisitor, e: &Expr, is_const: bool) {
143138
Some(&DefStruct(..)) => {} // OK.
144139
Some(&DefVariant(..)) => {} // OK.
145140
_ => {
146-
v.tcx.sess.span_err(e.span,
147-
"function calls in constants are limited to \
148-
struct and enum constructors");
141+
span_err!(v.tcx.sess, e.span, E0015,
142+
"function calls in constants are limited to struct and enum constructors");
149143
}
150144
}
151145
}
152146
ExprBlock(ref block) => {
153147
// Check all statements in the block
154148
for stmt in block.stmts.iter() {
155149
let block_span_err = |span|
156-
v.tcx.sess.span_err(span,
157-
"blocks in constants are limited to \
158-
items and tail expressions");
150+
span_err!(v.tcx.sess, span, E0016,
151+
"blocks in constants are limited to items and tail expressions");
159152
match stmt.node {
160153
StmtDecl(ref span, _) => {
161154
match span.node {
@@ -187,18 +180,18 @@ fn check_expr(v: &mut CheckCrateVisitor, e: &Expr, is_const: bool) {
187180
ExprRepeat(..) |
188181
ExprStruct(..) => { }
189182
ExprAddrOf(..) => {
190-
v.tcx.sess.span_err(e.span,
191-
"references in constants may only refer to \
192-
immutable values");
183+
span_err!(v.tcx.sess, e.span, E0017,
184+
"references in constants may only refer to immutable values");
193185
},
194186
ExprVstore(_, ExprVstoreUniq) => {
195-
v.tcx.sess.span_err(e.span, "cannot allocate vectors in constant expressions")
187+
span_err!(v.tcx.sess, e.span, E0018,
188+
"cannot allocate vectors in constant expressions");
196189
},
197190

198191
_ => {
199-
v.tcx.sess.span_err(e.span,
200-
"constant contains unimplemented expression type");
201-
return;
192+
span_err!(v.tcx.sess, e.span, E0019,
193+
"constant contains unimplemented expression type");
194+
return;
202195
}
203196
}
204197
}

src/librustc/middle/check_match.rs

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,16 @@ fn check_expr(cx: &mut MatchCheckCtxt, ex: &Expr) {
142142
// Finally, check if the whole match expression is exhaustive.
143143
// Check for empty enum, because is_useful only works on inhabited types.
144144
let pat_ty = node_id_to_type(cx.tcx, scrut.id);
145-
if (*arms).is_empty() {
146-
if !type_is_empty(cx.tcx, pat_ty) {
147-
// We know the type is inhabited, so this must be wrong
148-
cx.tcx.sess.span_err(ex.span, format!("non-exhaustive patterns: \
149-
type {} is non-empty",
150-
ty_to_string(cx.tcx, pat_ty)).as_slice());
151-
}
152-
// If the type *is* empty, it's vacuously exhaustive
153-
return;
145+
if arms.is_empty() {
146+
if !type_is_empty(cx.tcx, pat_ty) {
147+
// We know the type is inhabited, so this must be wrong
148+
span_err!(cx.tcx.sess, ex.span, E0002,
149+
"non-exhaustive patterns: type {} is non-empty",
150+
ty_to_string(cx.tcx, pat_ty)
151+
);
152+
}
153+
// If the type *is* empty, it's vacuously exhaustive
154+
return;
154155
}
155156
let m: Matrix = Matrix(arms
156157
.iter()
@@ -186,8 +187,9 @@ fn check_arms(cx: &MatchCheckCtxt, arms: &[Arm]) {
186187

187188
walk_pat(&**pat, |p| {
188189
if pat_matches_nan(p) {
189-
cx.tcx.sess.span_warn(p.span, "unmatchable NaN in pattern, \
190-
use the is_nan method in a guard instead");
190+
span_warn!(cx.tcx.sess, p.span, E0003,
191+
"unmatchable NaN in pattern, use the is_nan method in a guard instead"
192+
);
191193
}
192194
true
193195
});
@@ -222,9 +224,10 @@ fn check_exhaustive(cx: &MatchCheckCtxt, sp: Span, m: &Matrix) {
222224
[] => wild(),
223225
_ => unreachable!()
224226
};
225-
let msg = format!("non-exhaustive patterns: `{0}` not covered",
226-
pat_to_string(&*witness));
227-
cx.tcx.sess.span_err(sp, msg.as_slice());
227+
span_err!(cx.tcx.sess, sp, E0004,
228+
"non-exhaustive patterns: `{}` not covered",
229+
pat_to_string(&*witness)
230+
);
228231
}
229232
NotUseful => {
230233
// This is good, wildcard pattern isn't reachable
@@ -779,11 +782,10 @@ fn check_local(cx: &mut MatchCheckCtxt, loc: &Local) {
779782

780783
match is_refutable(cx, loc.pat) {
781784
Some(pat) => {
782-
let msg = format!(
785+
span_err!(cx.tcx.sess, loc.pat.span, E0005,
783786
"refutable pattern in {} binding: `{}` not covered",
784787
name, pat_to_string(&*pat)
785788
);
786-
cx.tcx.sess.span_err(loc.pat.span, msg.as_slice());
787789
},
788790
None => ()
789791
}
@@ -801,11 +803,10 @@ fn check_fn(cx: &mut MatchCheckCtxt,
801803
for input in decl.inputs.iter() {
802804
match is_refutable(cx, input.pat) {
803805
Some(pat) => {
804-
let msg = format!(
806+
span_err!(cx.tcx.sess, input.pat.span, E0006,
805807
"refutable pattern in function argument: `{}` not covered",
806808
pat_to_string(&*pat)
807809
);
808-
cx.tcx.sess.span_err(input.pat.span, msg.as_slice());
809810
},
810811
None => ()
811812
}
@@ -850,21 +851,13 @@ fn check_legality_of_move_bindings(cx: &MatchCheckCtxt,
850851

851852
// x @ Foo(..) is legal, but x @ Foo(y) isn't.
852853
if sub.map_or(false, |p| pat_contains_bindings(def_map, &*p)) {
853-
tcx.sess.span_err(
854-
p.span,
855-
"cannot bind by-move with sub-bindings");
854+
span_err!(cx.tcx.sess, p.span, E0007, "cannot bind by-move with sub-bindings");
856855
} else if has_guard {
857-
tcx.sess.span_err(
858-
p.span,
859-
"cannot bind by-move into a pattern guard");
856+
span_err!(cx.tcx.sess, p.span, E0008, "cannot bind by-move into a pattern guard");
860857
} else if by_ref_span.is_some() {
861-
tcx.sess.span_err(
862-
p.span,
863-
"cannot bind by-move and by-ref \
864-
in the same pattern");
865-
tcx.sess.span_note(
866-
by_ref_span.unwrap(),
867-
"by-ref binding occurs here");
858+
span_err!(cx.tcx.sess, p.span, E0009,
859+
"cannot bind by-move and by-ref in the same pattern");
860+
span_note!(cx.tcx.sess, by_ref_span.unwrap(), "by-ref binding occurs here");
868861
}
869862
};
870863

src/librustc/middle/check_static.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,18 @@ impl<'a> Visitor<bool> for CheckStaticVisitor<'a> {
112112
visit::walk_expr(self, e, is_const);
113113
}
114114
ast::ExprVstore(_, ast::ExprVstoreMutSlice) => {
115-
self.tcx.sess.span_err(e.span,
116-
"static items are not allowed to have mutable slices");
115+
span_err!(self.tcx.sess, e.span, E0020,
116+
"static items are not allowed to have mutable slices");
117117
},
118118
ast::ExprUnary(ast::UnBox, _) => {
119-
self.tcx.sess.span_err(e.span,
120-
"static items are not allowed to have managed pointers");
119+
span_err!(self.tcx.sess, e.span, E0021,
120+
"static items are not allowed to have managed pointers");
121121
}
122122
ast::ExprBox(..) |
123123
ast::ExprUnary(ast::UnUniq, _) |
124124
ast::ExprVstore(_, ast::ExprVstoreUniq) => {
125-
self.tcx.sess.span_err(e.span,
126-
"static items are not allowed to have custom pointers");
125+
span_err!(self.tcx.sess, e.span, E0022,
126+
"static items are not allowed to have custom pointers");
127127
}
128128
_ => {
129129
let node_ty = ty::node_id_to_type(self.tcx, e.id);

0 commit comments

Comments
 (0)