Skip to content

Commit 98b5285

Browse files
committed
Rollup merge of #29777 - Manishearth:const_irrefutable_diag, r=eddyb
None
2 parents 3ef3fd9 + 9be0ba5 commit 98b5285

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

src/librustc_resolve/lib.rs

+15-7
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ pub enum ResolutionError<'a> {
153153
/// error E0413: declaration shadows an enum variant or unit-like struct in scope
154154
DeclarationShadowsEnumVariantOrUnitLikeStruct(Name),
155155
/// error E0414: only irrefutable patterns allowed here
156-
OnlyIrrefutablePatternsAllowedHere,
156+
OnlyIrrefutablePatternsAllowedHere(DefId, Name),
157157
/// error E0415: identifier is bound more than once in this parameter list
158158
IdentifierBoundMoreThanOnceInParameterList(&'a str),
159159
/// error E0416: identifier is bound more than once in the same pattern
@@ -283,8 +283,16 @@ fn resolve_error<'b, 'a:'b, 'tcx:'a>(resolver: &'b Resolver<'a, 'tcx>, span: syn
283283
scope",
284284
name);
285285
},
286-
ResolutionError::OnlyIrrefutablePatternsAllowedHere => {
286+
ResolutionError::OnlyIrrefutablePatternsAllowedHere(did, name) => {
287287
span_err!(resolver.session, span, E0414, "only irrefutable patterns allowed here");
288+
resolver.session.span_note(span, "there already is a constant in scope sharing the same name as this pattern");
289+
if let Some(sp) = resolver.ast_map.span_if_local(did) {
290+
resolver.session.span_note(sp, "constant defined here");
291+
}
292+
if let Some(directive) = resolver.current_module.import_resolutions.borrow().get(&name) {
293+
let item = resolver.ast_map.expect_item(directive.value_id);
294+
resolver.session.span_note(item.span, "constant imported here");
295+
}
288296
},
289297
ResolutionError::IdentifierBoundMoreThanOnceInParameterList(identifier) => {
290298
span_err!(resolver.session, span, E0415,
@@ -632,7 +640,7 @@ enum NameSearchType {
632640
#[derive(Copy, Clone)]
633641
enum BareIdentifierPatternResolution {
634642
FoundStructOrEnumVariant(Def, LastPrivate),
635-
FoundConst(Def, LastPrivate),
643+
FoundConst(Def, LastPrivate, Name),
636644
BareIdentifierPatternUnresolved
637645
}
638646

@@ -2685,7 +2693,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
26852693
renamed)
26862694
);
26872695
}
2688-
FoundConst(def, lp) if const_ok => {
2696+
FoundConst(def, lp, _) if const_ok => {
26892697
debug!("(resolving pattern) resolving `{}` to \
26902698
constant",
26912699
renamed);
@@ -2700,11 +2708,11 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
27002708
depth: 0
27012709
});
27022710
}
2703-
FoundConst(..) => {
2711+
FoundConst(def, _, name) => {
27042712
resolve_error(
27052713
self,
27062714
pattern.span,
2707-
ResolutionError::OnlyIrrefutablePatternsAllowedHere
2715+
ResolutionError::OnlyIrrefutablePatternsAllowedHere(def.def_id(), name)
27082716
);
27092717
}
27102718
BareIdentifierPatternUnresolved => {
@@ -2929,7 +2937,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
29292937
return FoundStructOrEnumVariant(def, LastMod(AllPublic));
29302938
}
29312939
def @ DefConst(..) | def @ DefAssociatedConst(..) => {
2932-
return FoundConst(def, LastMod(AllPublic));
2940+
return FoundConst(def, LastMod(AllPublic), name);
29332941
}
29342942
DefStatic(..) => {
29352943
resolve_error(self,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
mod foo {
12+
const b: u8 = 2; //~ NOTE constant defined here
13+
const d: u8 = 2; //~ NOTE constant defined here
14+
}
15+
16+
use foo::b as c; //~ NOTE constant imported here
17+
use foo::d; //~ NOTE constant imported here
18+
19+
const a: u8 = 2; //~ NOTE constant defined here
20+
21+
fn main() {
22+
let a = 4; //~ ERROR only irrefutable
23+
//~^ NOTE there already is a constant in scope
24+
let c = 4; //~ ERROR only irrefutable
25+
//~^ NOTE there already is a constant in scope
26+
let d = 4; //~ ERROR only irrefutable
27+
//~^ NOTE there already is a constant in scope
28+
}

0 commit comments

Comments
 (0)