Skip to content

Commit 52c97f2

Browse files
committed
Rollup merge of #33340 - birkenfeld:issue-23716, r=Manishearth
resolve: print location of static for "static in pattern" error The implementation mirrors the one for "constant defined here" annotation used for constant patterns in the irrefutable-pattern case. Fixes: #23716
2 parents 041a269 + 4ba6bf4 commit 52c97f2

File tree

2 files changed

+54
-11
lines changed

2 files changed

+54
-11
lines changed

src/librustc_resolve/lib.rs

+25-11
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ enum ResolutionError<'a> {
147147
/// error E0416: identifier is bound more than once in the same pattern
148148
IdentifierBoundMoreThanOnceInSamePattern(&'a str),
149149
/// error E0417: static variables cannot be referenced in a pattern
150-
StaticVariableReference,
150+
StaticVariableReference(DefId, Option<Name>),
151151
/// error E0418: is not an enum variant, struct or const
152152
NotAnEnumVariantStructOrConst(&'a str),
153153
/// error E0419: unresolved enum variant, struct or const
@@ -352,12 +352,24 @@ fn resolve_struct_error<'b, 'a: 'b, 'tcx: 'a>(resolver: &'b Resolver<'a, 'tcx>,
352352
"identifier `{}` is bound more than once in the same pattern",
353353
identifier)
354354
}
355-
ResolutionError::StaticVariableReference => {
356-
struct_span_err!(resolver.session,
357-
span,
358-
E0417,
359-
"static variables cannot be referenced in a pattern, use a \
360-
`const` instead")
355+
ResolutionError::StaticVariableReference(did, name) => {
356+
let mut err = struct_span_err!(resolver.session,
357+
span,
358+
E0417,
359+
"static variables cannot be referenced in a \
360+
pattern, use a `const` instead");
361+
if let Some(sp) = resolver.ast_map.span_if_local(did) {
362+
err.span_note(sp, "static variable defined here");
363+
}
364+
if let Some(name) = name {
365+
if let Some(binding) = resolver.current_module
366+
.resolve_name_in_lexical_scope(name, ValueNS) {
367+
if binding.is_import() {
368+
err.span_note(binding.span, "static variable imported here");
369+
}
370+
}
371+
}
372+
err
361373
}
362374
ResolutionError::NotAnEnumVariantStructOrConst(name) => {
363375
struct_span_err!(resolver.session,
@@ -2313,10 +2325,11 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
23132325
Def::Variant(..) | Def::Const(..) => {
23142326
self.record_def(pattern.id, path_res);
23152327
}
2316-
Def::Static(..) => {
2328+
Def::Static(did, _) => {
23172329
resolve_error(&self,
23182330
path.span,
2319-
ResolutionError::StaticVariableReference);
2331+
ResolutionError::StaticVariableReference(
2332+
did, None));
23202333
self.record_def(pattern.id, err_path_resolution());
23212334
}
23222335
_ => {
@@ -2456,8 +2469,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
24562469
Some(def @ Def::Const(..)) | Some(def @ Def::AssociatedConst(..)) => {
24572470
FoundConst(def, ident.unhygienic_name)
24582471
}
2459-
Some(Def::Static(..)) => {
2460-
resolve_error(self, span, ResolutionError::StaticVariableReference);
2472+
Some(Def::Static(did, _)) => {
2473+
resolve_error(self, span, ResolutionError::StaticVariableReference(
2474+
did, Some(ident.unhygienic_name)));
24612475
BareIdentifierPatternUnresolved
24622476
}
24632477
_ => BareIdentifierPatternUnresolved,

src/test/compile-fail/issue-23716.rs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2016 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+
static foo: i32 = 0;
12+
//~^ NOTE static variable defined here
13+
14+
fn bar(foo: i32) {}
15+
//~^ ERROR static variables cannot be referenced in a pattern, use a `const` instead
16+
17+
mod submod {
18+
pub static answer: i32 = 42;
19+
//~^ NOTE static variable defined here
20+
}
21+
22+
use self::submod::answer;
23+
//~^ NOTE static variable imported here
24+
25+
fn question(answer: i32) {}
26+
//~^ ERROR static variables cannot be referenced in a pattern, use a `const` instead
27+
28+
fn main() {
29+
}

0 commit comments

Comments
 (0)