Skip to content

Commit 98df458

Browse files
committed
Auto merge of #28672 - sanxiyn:const-eval-span, r=alexcrichton
Fix #28402.
2 parents bfb2603 + 4fb789b commit 98df458

File tree

5 files changed

+37
-9
lines changed

5 files changed

+37
-9
lines changed

src/librustc/middle/check_match.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,10 @@ fn check_for_static_nan(cx: &MatchCheckCtxt, pat: &Pat) {
281281
Ok(_) => {}
282282

283283
Err(err) => {
284-
let subspan = p.span.lo <= err.span.lo && err.span.hi <= p.span.hi;
285284
span_err!(cx.tcx.sess, err.span, E0471,
286285
"constant evaluation error: {}",
287286
err.description());
288-
if !subspan {
287+
if !p.span.contains(err.span) {
289288
cx.tcx.sess.span_note(p.span,
290289
"in pattern here")
291290
}

src/librustc_typeck/astconv.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1692,12 +1692,10 @@ pub fn ast_ty_to_ty<'tcx>(this: &AstConv<'tcx>,
16921692
}
16931693
}
16941694
Err(ref r) => {
1695-
let subspan =
1696-
ast_ty.span.lo <= r.span.lo && r.span.hi <= ast_ty.span.hi;
16971695
span_err!(tcx.sess, r.span, E0250,
16981696
"array length constant evaluation error: {}",
16991697
r.description());
1700-
if !subspan {
1698+
if !ast_ty.span.contains(r.span) {
17011699
span_note!(tcx.sess, ast_ty.span, "for array length here")
17021700
}
17031701
this.tcx().types.err

src/librustc_typeck/collect.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,9 +1173,12 @@ fn convert_enum_def<'tcx>(tcx: &ty::ctxt<'tcx>,
11731173
None
11741174
},
11751175
Err(err) => {
1176-
span_err!(tcx.sess, err.span, E0080,
1177-
"constant evaluation error: {}",
1178-
err.description());
1176+
span_err!(tcx.sess, err.span, E0080,
1177+
"constant evaluation error: {}",
1178+
err.description());
1179+
if !e.span.contains(err.span) {
1180+
tcx.sess.span_note(e.span, "for enum discriminant here");
1181+
}
11791182
None
11801183
}
11811184
}

src/libsyntax/codemap.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ impl Span {
142142
pub fn substitute_dummy(self, other: Span) -> Span {
143143
if self == DUMMY_SP { other } else { self }
144144
}
145+
146+
pub fn contains(self, other: Span) -> bool {
147+
self.lo <= other.lo && other.hi <= self.hi
148+
}
145149
}
146150

147151
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
@@ -1011,7 +1015,7 @@ impl CodeMap {
10111015

10121016
let span_comes_from_this_expansion =
10131017
info.callee.span.map_or(span == info.call_site, |mac_span| {
1014-
mac_span.lo <= span.lo && span.hi <= mac_span.hi
1018+
mac_span.contains(span)
10151019
});
10161020

10171021
debug!("span_allows_unstable: span: {:?} call_site: {:?} callee: {:?}",
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
// Check that error in constant evaluation of enum discriminant
12+
// provides the context for what caused the evaluation.
13+
14+
struct S(i32);
15+
16+
const CONSTANT: S = S(0);
17+
//~^ ERROR: constant evaluation error: unsupported constant expr
18+
19+
enum E {
20+
V = CONSTANT,
21+
//~^ NOTE: for enum discriminant here
22+
}
23+
24+
fn main() {}

0 commit comments

Comments
 (0)