Skip to content

Commit 4ba6bf4

Browse files
committed
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
1 parent 855fb61 commit 4ba6bf4

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
@@ -158,7 +158,7 @@ enum ResolutionError<'a> {
158158
/// error E0416: identifier is bound more than once in the same pattern
159159
IdentifierBoundMoreThanOnceInSamePattern(&'a str),
160160
/// error E0417: static variables cannot be referenced in a pattern
161-
StaticVariableReference,
161+
StaticVariableReference(DefId, Option<Name>),
162162
/// error E0418: is not an enum variant, struct or const
163163
NotAnEnumVariantStructOrConst(&'a str),
164164
/// error E0419: unresolved enum variant, struct or const
@@ -367,12 +367,24 @@ fn resolve_struct_error<'b, 'a: 'b, 'tcx: 'a>(resolver: &'b Resolver<'a, 'tcx>,
367367
"identifier `{}` is bound more than once in the same pattern",
368368
identifier)
369369
}
370-
ResolutionError::StaticVariableReference => {
371-
struct_span_err!(resolver.session,
372-
span,
373-
E0417,
374-
"static variables cannot be referenced in a pattern, use a \
375-
`const` instead")
370+
ResolutionError::StaticVariableReference(did, name) => {
371+
let mut err = struct_span_err!(resolver.session,
372+
span,
373+
E0417,
374+
"static variables cannot be referenced in a \
375+
pattern, use a `const` instead");
376+
if let Some(sp) = resolver.ast_map.span_if_local(did) {
377+
err.span_note(sp, "static variable defined here");
378+
}
379+
if let Some(name) = name {
380+
if let Some(binding) = resolver.current_module
381+
.resolve_name_in_lexical_scope(name, ValueNS) {
382+
if binding.is_import() {
383+
err.span_note(binding.span, "static variable imported here");
384+
}
385+
}
386+
}
387+
err
376388
}
377389
ResolutionError::NotAnEnumVariantStructOrConst(name) => {
378390
struct_span_err!(resolver.session,
@@ -2374,10 +2386,11 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
23742386
Def::Variant(..) | Def::Const(..) => {
23752387
self.record_def(pattern.id, path_res);
23762388
}
2377-
Def::Static(..) => {
2389+
Def::Static(did, _) => {
23782390
resolve_error(&self,
23792391
path.span,
2380-
ResolutionError::StaticVariableReference);
2392+
ResolutionError::StaticVariableReference(
2393+
did, None));
23812394
self.record_def(pattern.id, err_path_resolution());
23822395
}
23832396
_ => {
@@ -2517,8 +2530,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
25172530
Some(def @ Def::Const(..)) | Some(def @ Def::AssociatedConst(..)) => {
25182531
FoundConst(def, ident.unhygienic_name)
25192532
}
2520-
Some(Def::Static(..)) => {
2521-
resolve_error(self, span, ResolutionError::StaticVariableReference);
2533+
Some(Def::Static(did, _)) => {
2534+
resolve_error(self, span, ResolutionError::StaticVariableReference(
2535+
did, Some(ident.unhygienic_name)));
25222536
BareIdentifierPatternUnresolved
25232537
}
25242538
_ => 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)