Skip to content

Commit f2eefe3

Browse files
committed
Replace HashMap with IndexMap in pattern binding resolve
It will be iterated over, so we should avoid using `HashMap`.
1 parent 551c718 commit f2eefe3

File tree

4 files changed

+85
-90
lines changed

4 files changed

+85
-90
lines changed

compiler/rustc_resolve/src/late.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -3201,8 +3201,8 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
32013201
/// Checks that all of the arms in an or-pattern have exactly the
32023202
/// same set of bindings, with the same binding modes for each.
32033203
fn check_consistent_bindings(&mut self, pats: &[P<Pat>]) -> Vec<BindingMap> {
3204-
let mut missing_vars = FxHashMap::default();
3205-
let mut inconsistent_vars = FxHashMap::default();
3204+
let mut missing_vars = FxIndexMap::default();
3205+
let mut inconsistent_vars = FxIndexMap::default();
32063206

32073207
// 1) Compute the binding maps of all arms.
32083208
let maps = pats.iter().map(|pat| self.binding_mode_map(pat)).collect::<Vec<_>>();
@@ -3244,10 +3244,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
32443244
}
32453245

32463246
// 3) Report all missing variables we found.
3247-
let mut missing_vars = missing_vars.into_iter().collect::<Vec<_>>();
3248-
missing_vars.sort_by_key(|&(sym, ref _err)| sym);
3249-
3250-
for (name, mut v) in missing_vars.into_iter() {
3247+
for (name, mut v) in missing_vars {
32513248
if inconsistent_vars.contains_key(&name) {
32523249
v.could_be_path = false;
32533250
}
@@ -3258,8 +3255,6 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
32583255
}
32593256

32603257
// 4) Report all inconsistencies in binding modes we found.
3261-
let mut inconsistent_vars = inconsistent_vars.iter().collect::<Vec<_>>();
3262-
inconsistent_vars.sort();
32633258
for (name, v) in inconsistent_vars {
32643259
self.report_error(v.0, ResolutionError::VariableBoundWithDifferentMode(*name, v.1));
32653260
}

tests/ui/or-patterns/missing-bindings.stderr

+44-44
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ LL | let (A(A(a, b) | B(c), d) | B(e)) = Y;
8787
| |
8888
| pattern doesn't bind `c`
8989

90+
error[E0408]: variable `b` is not bound in all patterns
91+
--> $DIR/missing-bindings.rs:45:22
92+
|
93+
LL | let (A(A(a, b) | B(c), d) | B(e)) = Y;
94+
| - ^^^^ pattern doesn't bind `b`
95+
| |
96+
| variable not in all patterns
97+
9098
error[E0408]: variable `a` is not bound in all patterns
9199
--> $DIR/missing-bindings.rs:45:22
92100
|
@@ -95,11 +103,19 @@ LL | let (A(A(a, b) | B(c), d) | B(e)) = Y;
95103
| |
96104
| variable not in all patterns
97105

106+
error[E0408]: variable `e` is not bound in all patterns
107+
--> $DIR/missing-bindings.rs:45:10
108+
|
109+
LL | let (A(A(a, b) | B(c), d) | B(e)) = Y;
110+
| ^^^^^^^^^^^^^^^^^^^^ - variable not in all patterns
111+
| |
112+
| pattern doesn't bind `e`
113+
98114
error[E0408]: variable `b` is not bound in all patterns
99-
--> $DIR/missing-bindings.rs:45:22
115+
--> $DIR/missing-bindings.rs:45:33
100116
|
101117
LL | let (A(A(a, b) | B(c), d) | B(e)) = Y;
102-
| - ^^^^ pattern doesn't bind `b`
118+
| - ^^^^ pattern doesn't bind `b`
103119
| |
104120
| variable not in all patterns
105121

@@ -119,14 +135,6 @@ LL | let (A(A(a, b) | B(c), d) | B(e)) = Y;
119135
| |
120136
| variable not in all patterns
121137

122-
error[E0408]: variable `e` is not bound in all patterns
123-
--> $DIR/missing-bindings.rs:45:10
124-
|
125-
LL | let (A(A(a, b) | B(c), d) | B(e)) = Y;
126-
| ^^^^^^^^^^^^^^^^^^^^ - variable not in all patterns
127-
| |
128-
| pattern doesn't bind `e`
129-
130138
error[E0408]: variable `a` is not bound in all patterns
131139
--> $DIR/missing-bindings.rs:45:33
132140
|
@@ -135,14 +143,6 @@ LL | let (A(A(a, b) | B(c), d) | B(e)) = Y;
135143
| |
136144
| variable not in all patterns
137145

138-
error[E0408]: variable `b` is not bound in all patterns
139-
--> $DIR/missing-bindings.rs:45:33
140-
|
141-
LL | let (A(A(a, b) | B(c), d) | B(e)) = Y;
142-
| - ^^^^ pattern doesn't bind `b`
143-
| |
144-
| variable not in all patterns
145-
146146
error[E0408]: variable `a` is not bound in all patterns
147147
--> $DIR/missing-bindings.rs:61:29
148148
|
@@ -151,6 +151,14 @@ LL | Ok(a) | Err(_),
151151
| |
152152
| variable not in all patterns
153153

154+
error[E0408]: variable `b` is not bound in all patterns
155+
--> $DIR/missing-bindings.rs:68:21
156+
|
157+
LL | A(_, a) |
158+
| ^^^^^^^ pattern doesn't bind `b`
159+
LL | B(b),
160+
| - variable not in all patterns
161+
154162
error[E0408]: variable `a` is not bound in all patterns
155163
--> $DIR/missing-bindings.rs:69:21
156164
|
@@ -160,12 +168,13 @@ LL | B(b),
160168
| ^^^^ pattern doesn't bind `a`
161169

162170
error[E0408]: variable `b` is not bound in all patterns
163-
--> $DIR/missing-bindings.rs:68:21
171+
--> $DIR/missing-bindings.rs:72:17
164172
|
165-
LL | A(_, a) |
166-
| ^^^^^^^ pattern doesn't bind `b`
167173
LL | B(b),
168174
| - variable not in all patterns
175+
...
176+
LL | B(_)
177+
| ^^^^ pattern doesn't bind `b`
169178

170179
error[E0408]: variable `a` is not bound in all patterns
171180
--> $DIR/missing-bindings.rs:72:17
@@ -177,13 +186,22 @@ LL | B(_)
177186
| ^^^^ pattern doesn't bind `a`
178187

179188
error[E0408]: variable `b` is not bound in all patterns
180-
--> $DIR/missing-bindings.rs:72:17
189+
--> $DIR/missing-bindings.rs:57:13
181190
|
182-
LL | B(b),
183-
| - variable not in all patterns
191+
LL | / V1(
192+
LL | |
193+
LL | |
194+
LL | | A(
195+
... |
196+
LL | | B(Ok(a) | Err(a))
197+
LL | | ) |
198+
| |_____________^ pattern doesn't bind `b`
184199
...
185-
LL | B(_)
186-
| ^^^^ pattern doesn't bind `b`
200+
LL | B(b),
201+
| - variable not in all patterns
202+
...
203+
LL | V3(c),
204+
| ^^^^^ pattern doesn't bind `b`
187205

188206
error[E0408]: variable `c` is not bound in all patterns
189207
--> $DIR/missing-bindings.rs:57:13
@@ -219,24 +237,6 @@ LL | A(_, a) |
219237
LL | V3(c),
220238
| ^^^^^ pattern doesn't bind `a`
221239

222-
error[E0408]: variable `b` is not bound in all patterns
223-
--> $DIR/missing-bindings.rs:57:13
224-
|
225-
LL | / V1(
226-
LL | |
227-
LL | |
228-
LL | | A(
229-
... |
230-
LL | | B(Ok(a) | Err(a))
231-
LL | | ) |
232-
| |_____________^ pattern doesn't bind `b`
233-
...
234-
LL | B(b),
235-
| - variable not in all patterns
236-
...
237-
LL | V3(c),
238-
| ^^^^^ pattern doesn't bind `b`
239-
240240
error: aborting due to 26 previous errors
241241

242242
For more information about this error, try `rustc --explain E0408`.

tests/ui/resolve/resolve-inconsistent-names.stderr

+21-21
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
error[E0408]: variable `a` is not bound in all patterns
2-
--> $DIR/resolve-inconsistent-names.rs:13:12
3-
|
4-
LL | a | b => {}
5-
| - ^ pattern doesn't bind `a`
6-
| |
7-
| variable not in all patterns
8-
91
error[E0408]: variable `b` is not bound in all patterns
102
--> $DIR/resolve-inconsistent-names.rs:13:8
113
|
@@ -14,6 +6,14 @@ LL | a | b => {}
146
| |
157
| pattern doesn't bind `b`
168

9+
error[E0408]: variable `a` is not bound in all patterns
10+
--> $DIR/resolve-inconsistent-names.rs:13:12
11+
|
12+
LL | a | b => {}
13+
| - ^ pattern doesn't bind `a`
14+
| |
15+
| variable not in all patterns
16+
1717
error[E0408]: variable `c` is not bound in all patterns
1818
--> $DIR/resolve-inconsistent-names.rs:19:9
1919
|
@@ -54,6 +54,19 @@ LL | (A, B) | (ref B, c) | (c, A) => ()
5454
| |
5555
| first binding
5656

57+
error[E0408]: variable `Const2` is not bound in all patterns
58+
--> $DIR/resolve-inconsistent-names.rs:31:9
59+
|
60+
LL | (CONST1, _) | (_, Const2) => ()
61+
| ^^^^^^^^^^^ ------ variable not in all patterns
62+
| |
63+
| pattern doesn't bind `Const2`
64+
|
65+
help: if you meant to match on constant `m::Const2`, use the full path in the pattern
66+
|
67+
LL | (CONST1, _) | (_, m::Const2) => ()
68+
| ~~~~~~~~~
69+
5770
error[E0408]: variable `CONST1` is not bound in all patterns
5871
--> $DIR/resolve-inconsistent-names.rs:31:23
5972
|
@@ -68,19 +81,6 @@ note: you might have meant to match on constant `m::CONST1`, which exists but is
6881
LL | const CONST1: usize = 10;
6982
| ^^^^^^^^^^^^^^^^^^^^^^^^^ not accessible
7083

71-
error[E0408]: variable `Const2` is not bound in all patterns
72-
--> $DIR/resolve-inconsistent-names.rs:31:9
73-
|
74-
LL | (CONST1, _) | (_, Const2) => ()
75-
| ^^^^^^^^^^^ ------ variable not in all patterns
76-
| |
77-
| pattern doesn't bind `Const2`
78-
|
79-
help: if you meant to match on constant `m::Const2`, use the full path in the pattern
80-
|
81-
LL | (CONST1, _) | (_, m::Const2) => ()
82-
| ~~~~~~~~~
83-
8484
error[E0308]: mismatched types
8585
--> $DIR/resolve-inconsistent-names.rs:19:19
8686
|

tests/ui/span/issue-39698.stderr

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
error[E0408]: variable `b` is not bound in all patterns
2+
--> $DIR/issue-39698.rs:10:9
3+
|
4+
LL | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => { println!("{:?}", a); }
5+
| ^^^^^^^^^^^ - ^^^^^^^^ ^^^^^^^^ pattern doesn't bind `b`
6+
| | | |
7+
| | | pattern doesn't bind `b`
8+
| | variable not in all patterns
9+
| pattern doesn't bind `b`
10+
111
error[E0408]: variable `c` is not bound in all patterns
212
--> $DIR/issue-39698.rs:10:9
313
|
@@ -8,16 +18,6 @@ LL | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => { println!("{:?}
818
| | pattern doesn't bind `c`
919
| pattern doesn't bind `c`
1020

11-
error[E0408]: variable `d` is not bound in all patterns
12-
--> $DIR/issue-39698.rs:10:37
13-
|
14-
LL | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => { println!("{:?}", a); }
15-
| - - ^^^^^^^^ ^^^^^^^^ pattern doesn't bind `d`
16-
| | | |
17-
| | | pattern doesn't bind `d`
18-
| | variable not in all patterns
19-
| variable not in all patterns
20-
2121
error[E0408]: variable `a` is not bound in all patterns
2222
--> $DIR/issue-39698.rs:10:23
2323
|
@@ -28,15 +28,15 @@ LL | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => { println!("{:?}
2828
| | pattern doesn't bind `a`
2929
| variable not in all patterns
3030

31-
error[E0408]: variable `b` is not bound in all patterns
32-
--> $DIR/issue-39698.rs:10:9
31+
error[E0408]: variable `d` is not bound in all patterns
32+
--> $DIR/issue-39698.rs:10:37
3333
|
3434
LL | T::T1(a, d) | T::T2(d, b) | T::T3(c) | T::T4(a) => { println!("{:?}", a); }
35-
| ^^^^^^^^^^^ - ^^^^^^^^ ^^^^^^^^ pattern doesn't bind `b`
36-
| | | |
37-
| | | pattern doesn't bind `b`
38-
| | variable not in all patterns
39-
| pattern doesn't bind `b`
35+
| - - ^^^^^^^^ ^^^^^^^^ pattern doesn't bind `d`
36+
| | | |
37+
| | | pattern doesn't bind `d`
38+
| | variable not in all patterns
39+
| variable not in all patterns
4040

4141
error: aborting due to 4 previous errors
4242

0 commit comments

Comments
 (0)