Skip to content

Commit db0be32

Browse files
committed
resolve: Use same rules for disambiguating fresh bindings in match and let
1 parent 2278506 commit db0be32

File tree

6 files changed

+90
-25
lines changed

6 files changed

+90
-25
lines changed

src/librustc_resolve/lib.rs

+11-13
Original file line numberDiff line numberDiff line change
@@ -376,12 +376,6 @@ enum PatternSource {
376376
}
377377

378378
impl PatternSource {
379-
fn is_refutable(self) -> bool {
380-
match self {
381-
PatternSource::Match | PatternSource::IfLet | PatternSource::WhileLet => true,
382-
PatternSource::Let | PatternSource::For | PatternSource::FnParam => false,
383-
}
384-
}
385379
fn descr(self) -> &'static str {
386380
match self {
387381
PatternSource::Match => "match binding",
@@ -2378,20 +2372,24 @@ impl<'a> Resolver<'a> {
23782372
false, pat.span)
23792373
.and_then(LexicalScopeBinding::item);
23802374
let resolution = binding.map(NameBinding::def).and_then(|def| {
2381-
let ivmode = BindingMode::ByValue(Mutability::Immutable);
2382-
let always_binding = !pat_src.is_refutable() || opt_pat.is_some() ||
2383-
bmode != ivmode;
2375+
let is_syntactic_ambiguity = opt_pat.is_none() &&
2376+
bmode == BindingMode::ByValue(Mutability::Immutable);
23842377
match def {
23852378
Def::StructCtor(_, CtorKind::Const) |
23862379
Def::VariantCtor(_, CtorKind::Const) |
2387-
Def::Const(..) if !always_binding => {
2388-
// A unit struct/variant or constant pattern.
2380+
Def::Const(..) if is_syntactic_ambiguity => {
2381+
// Disambiguate in favor of a unit struct/variant
2382+
// or constant pattern.
23892383
self.record_use(ident.node, ValueNS, binding.unwrap(), ident.span);
23902384
Some(PathResolution::new(def))
23912385
}
23922386
Def::StructCtor(..) | Def::VariantCtor(..) |
23932387
Def::Const(..) | Def::Static(..) => {
2394-
// A fresh binding that shadows something unacceptable.
2388+
// This is unambiguously a fresh binding, either syntactically
2389+
// (e.g. `IDENT @ PAT` or `ref IDENT`) or because `IDENT` resolves
2390+
// to something unusable as a pattern (e.g. constructor function),
2391+
// but we still conservatively report an error, see
2392+
// issues/33118#issuecomment-233962221 for one reason why.
23952393
resolve_error(
23962394
self,
23972395
ident.span,
@@ -2400,7 +2398,7 @@ impl<'a> Resolver<'a> {
24002398
);
24012399
None
24022400
}
2403-
Def::Local(..) | Def::Upvar(..) | Def::Fn(..) | Def::Err => {
2401+
Def::Fn(..) | Def::Err => {
24042402
// These entities are explicitly allowed
24052403
// to be shadowed by fresh bindings.
24062404
None

src/test/compile-fail/blind-item-block-middle.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ mod foo { pub struct bar; }
1212

1313
fn main() {
1414
let bar = 5;
15-
//~^ ERROR let bindings cannot shadow unit structs
15+
//~^ ERROR mismatched types
1616
use foo::bar;
1717
}

src/test/compile-fail/const-pattern-irrefutable.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ mod foo {
1313
pub const d: u8 = 2;
1414
}
1515

16-
use foo::b as c; //~ NOTE is imported here
17-
use foo::d; //~ NOTE is imported here
16+
use foo::b as c;
17+
use foo::d;
1818

19-
const a: u8 = 2; //~ NOTE is defined here
19+
const a: u8 = 2;
2020

2121
fn main() {
22-
let a = 4; //~ ERROR let bindings cannot shadow constants
23-
//~^ NOTE cannot be named the same as a constant
24-
let c = 4; //~ ERROR let bindings cannot shadow constants
25-
//~^ NOTE cannot be named the same as a constant
26-
let d = 4; //~ ERROR let bindings cannot shadow constants
27-
//~^ NOTE cannot be named the same as a constant
22+
let a = 4; //~ ERROR refutable pattern in local binding: `_` not covered
23+
//~^ NOTE pattern `_` not covered
24+
let c = 4; //~ ERROR refutable pattern in local binding: `_` not covered
25+
//~^ NOTE pattern `_` not covered
26+
let d = 4; //~ ERROR refutable pattern in local binding: `_` not covered
27+
//~^ NOTE pattern `_` not covered
2828
fn f() {} // Check that the `NOTE`s still work with an item here (c.f. issue #35115).
2929
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ struct Test;
1414

1515
fn main() {
1616
|| {
17-
let Test = 1; //~ ERROR let bindings cannot shadow unit structs
17+
let Test = 1; //~ ERROR mismatched types
1818
};
1919
}

src/test/compile-fail/name-clash-nullary.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use std::option::*;
1212

1313
fn main() {
14-
let None: isize = 42; //~ ERROR let bindings cannot shadow unit variants
14+
let None: isize = 42; //~ ERROR mismatched types
1515
log(debug, None);
1616
//~^ ERROR cannot find function `log` in this scope
1717
//~| ERROR cannot find value `debug` in this scope
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2017 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+
struct UnitStruct;
12+
struct TupleStruct();
13+
struct BracedStruct{}
14+
15+
enum E {
16+
UnitVariant,
17+
TupleVariant(),
18+
BracedVariant{},
19+
}
20+
use E::*;
21+
22+
const CONST: () = ();
23+
static STATIC: () = ();
24+
25+
fn function() {}
26+
27+
fn main() {
28+
let doesnt_matter = 0;
29+
30+
match UnitStruct {
31+
UnitStruct => {} // OK, `UnitStruct` is a unit struct pattern
32+
}
33+
match doesnt_matter {
34+
TupleStruct => {} //~ ERROR match bindings cannot shadow tuple structs
35+
}
36+
match doesnt_matter {
37+
BracedStruct => {} // OK, `BracedStruct` is a fresh binding
38+
}
39+
match UnitVariant {
40+
UnitVariant => {} // OK, `UnitVariant` is a unit variant pattern
41+
}
42+
match doesnt_matter {
43+
TupleVariant => {} //~ ERROR match bindings cannot shadow tuple variants
44+
}
45+
match doesnt_matter {
46+
BracedVariant => {} //~ ERROR match bindings cannot shadow struct variants
47+
}
48+
match CONST {
49+
CONST => {} // OK, `CONST` is a const pattern
50+
}
51+
match doesnt_matter {
52+
STATIC => {} //~ ERROR match bindings cannot shadow statics
53+
}
54+
match doesnt_matter {
55+
function => {} // OK, `function` is a fresh binding
56+
}
57+
58+
let UnitStruct = UnitStruct; // OK, `UnitStruct` is a unit struct pattern
59+
let TupleStruct = doesnt_matter; //~ ERROR let bindings cannot shadow tuple structs
60+
let BracedStruct = doesnt_matter; // OK, `BracedStruct` is a fresh binding
61+
let UnitVariant = UnitVariant; // OK, `UnitVariant` is a unit variant pattern
62+
let TupleVariant = doesnt_matter; //~ ERROR let bindings cannot shadow tuple variants
63+
let BracedVariant = doesnt_matter; //~ ERROR let bindings cannot shadow struct variants
64+
let CONST = CONST; // OK, `CONST` is a const pattern
65+
let STATIC = doesnt_matter; //~ ERROR let bindings cannot shadow statics
66+
let function = doesnt_matter; // OK, `function` is a fresh binding
67+
}

0 commit comments

Comments
 (0)