@@ -2180,7 +2180,8 @@ impl<'a> Resolver<'a> {
2180
2180
// because that breaks the assumptions later
2181
2181
// passes make about or-patterns.)
2182
2182
let renamed = mtwt:: resolve ( ident. node ) ;
2183
- let def = match bindings. get ( & renamed) . cloned ( ) {
2183
+ let mut def = Def :: Local ( self . definitions . local_def_id ( pat_id) , pat_id) ;
2184
+ match bindings. get ( & renamed) . cloned ( ) {
2184
2185
Some ( id) if id == outer_pat_id => {
2185
2186
// `Variant(a, a)`, error
2186
2187
resolve_error (
@@ -2189,7 +2190,6 @@ impl<'a> Resolver<'a> {
2189
2190
ResolutionError :: IdentifierBoundMoreThanOnceInSamePattern (
2190
2191
& ident. node . name . as_str ( ) )
2191
2192
) ;
2192
- Def :: Err
2193
2193
}
2194
2194
Some ( ..) if pat_src == PatternSource :: FnParam => {
2195
2195
// `fn f(a: u8, a: u8)`, error
@@ -2199,29 +2199,24 @@ impl<'a> Resolver<'a> {
2199
2199
ResolutionError :: IdentifierBoundMoreThanOnceInParameterList (
2200
2200
& ident. node . name . as_str ( ) )
2201
2201
) ;
2202
- Def :: Err
2203
2202
}
2204
2203
Some ( ..) if pat_src == PatternSource :: Match => {
2205
2204
// `Variant1(a) | Variant2(a)`, ok
2206
2205
// Reuse definition from the first `a`.
2207
- self . value_ribs . last_mut ( ) . unwrap ( ) . bindings [ & renamed]
2206
+ def = self . value_ribs . last_mut ( ) . unwrap ( ) . bindings [ & renamed] ;
2208
2207
}
2209
2208
Some ( ..) => {
2210
2209
span_bug ! ( ident. span, "two bindings with the same name from \
2211
2210
unexpected pattern source {:?}", pat_src) ;
2212
2211
}
2213
2212
None => {
2214
- // A completely fresh binding, add to the lists.
2215
- // FIXME: Later stages are not ready to deal with `Def::Err` here yet, so
2216
- // define `Invalid` bindings as `Def::Local`, just don't add them to the lists.
2217
- let def = Def :: Local ( self . definitions . local_def_id ( pat_id) , pat_id) ;
2213
+ // A completely fresh binding, add to the lists if it's valid.
2218
2214
if ident. node . name != keywords:: Invalid . name ( ) {
2219
2215
bindings. insert ( renamed, outer_pat_id) ;
2220
2216
self . value_ribs . last_mut ( ) . unwrap ( ) . bindings . insert ( renamed, def) ;
2221
2217
}
2222
- def
2223
2218
}
2224
- } ;
2219
+ }
2225
2220
2226
2221
PathResolution :: new ( def)
2227
2222
}
@@ -2287,15 +2282,16 @@ impl<'a> Resolver<'a> {
2287
2282
PatKind :: Ident ( bmode, ref ident, ref opt_pat) => {
2288
2283
// First try to resolve the identifier as some existing
2289
2284
// entity, then fall back to a fresh binding.
2290
- let local_def = self . resolve_identifier ( ident. node , ValueNS , true ) ;
2291
- let resolution = if let Some ( LocalDef { def, .. } ) = local_def {
2285
+ let resolution = self . resolve_identifier ( ident. node , ValueNS , true )
2286
+ . map ( |local_def| PathResolution :: new ( local_def. def ) )
2287
+ . and_then ( |resolution| {
2292
2288
let always_binding = !pat_src. is_refutable ( ) || opt_pat. is_some ( ) ||
2293
2289
bmode != BindingMode :: ByValue ( Mutability :: Immutable ) ;
2294
2290
match def {
2295
2291
Def :: Struct ( ..) | Def :: Variant ( ..) |
2296
2292
Def :: Const ( ..) | Def :: AssociatedConst ( ..) if !always_binding => {
2297
2293
// A constant, unit variant, etc pattern.
2298
- PathResolution :: new ( def )
2294
+ Some ( resolution )
2299
2295
}
2300
2296
Def :: Struct ( ..) | Def :: Variant ( ..) |
2301
2297
Def :: Const ( ..) | Def :: AssociatedConst ( ..) | Def :: Static ( ..) => {
@@ -2307,23 +2303,21 @@ impl<'a> Resolver<'a> {
2307
2303
ResolutionError :: BindingShadowsSomethingUnacceptable (
2308
2304
pat_src. descr ( ) , kind_name, ident. node . name )
2309
2305
) ;
2310
- err_path_resolution ( )
2306
+ None
2311
2307
}
2312
2308
Def :: Local ( ..) | Def :: Upvar ( ..) | Def :: Fn ( ..) => {
2313
2309
// These entities are explicitly allowed
2314
2310
// to be shadowed by fresh bindings.
2315
- self . fresh_binding ( ident, pat. id , outer_pat_id,
2316
- pat_src, bindings)
2311
+ None
2317
2312
}
2318
2313
def => {
2319
2314
span_bug ! ( ident. span, "unexpected definition for an \
2320
2315
identifier in pattern {:?}", def) ;
2321
2316
}
2322
2317
}
2323
- } else {
2324
- // Fall back to a fresh binding.
2318
+ } ) . unwrap_or_else ( || {
2325
2319
self . fresh_binding ( ident, pat. id , outer_pat_id, pat_src, bindings)
2326
- } ;
2320
+ } ) ;
2327
2321
2328
2322
self . record_def ( pat. id , resolution) ;
2329
2323
}
0 commit comments