2
2
3
3
use std:: cell:: Cell ;
4
4
use std:: mem;
5
- use std:: task:: Poll ;
6
5
7
6
use rustc_ast:: NodeId ;
8
7
use rustc_data_structures:: fx:: { FxHashSet , FxIndexSet } ;
@@ -40,9 +39,22 @@ use crate::{
40
39
41
40
type Res = def:: Res < NodeId > ;
42
41
43
- // Poll is a weird name.
44
- // FIXME: define/use something else because we are not doing async.
45
- pub ( crate ) use std:: task:: Poll as Progress ;
42
+ /// A [`NameBinding`] in the process of being resolved.
43
+ #[ derive( Clone , Copy , Default , PartialEq ) ]
44
+ pub ( crate ) enum PendingBinding < ' ra > {
45
+ Ready ( Option < NameBinding < ' ra > > ) ,
46
+ #[ default]
47
+ Pending ,
48
+ }
49
+
50
+ impl < ' ra > PendingBinding < ' ra > {
51
+ pub ( crate ) fn binding ( self ) -> Option < NameBinding < ' ra > > {
52
+ match self {
53
+ PendingBinding :: Ready ( binding) => binding,
54
+ PendingBinding :: Pending => None ,
55
+ }
56
+ }
57
+ }
46
58
47
59
/// Contains data for specific kinds of imports.
48
60
#[ derive( Clone ) ]
@@ -54,11 +66,7 @@ pub(crate) enum ImportKind<'ra> {
54
66
/// It will directly use `source` when the format is `use prefix::source`.
55
67
target : Ident ,
56
68
/// Bindings introduced by the import.
57
- /// `Progress` (Poll) because it's clearer semantically:
58
- /// - `Progress::Ready(Some)`: Determined and resolved
59
- /// - `Progress::Ready(None)`: Determined and not resolved
60
- /// - `Progress::Pending`: Undetermined
61
- bindings : PerNS < Cell < Progress < Option < NameBinding < ' ra > > > > > ,
69
+ bindings : PerNS < Cell < PendingBinding < ' ra > > > ,
62
70
/// `true` for `...::{self [as target]}` imports, `false` otherwise.
63
71
type_ns_only : bool ,
64
72
/// Did this import result from a nested import? ie. `use foo::{bar, baz};`
@@ -109,7 +117,7 @@ impl<'ra> std::fmt::Debug for ImportKind<'ra> {
109
117
// Ignore the nested bindings to avoid an infinite loop while printing.
110
118
. field (
111
119
"bindings" ,
112
- & bindings. clone ( ) . map ( |b| b. into_inner ( ) . map ( |_| format_args ! ( ".." ) ) ) ,
120
+ & bindings. clone ( ) . map ( |b| b. into_inner ( ) . binding ( ) . map ( |_| format_args ! ( ".." ) ) ) ,
113
121
)
114
122
. field ( "type_ns_only" , type_ns_only)
115
123
. field ( "nested" , nested)
@@ -487,9 +495,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
487
495
fn import_dummy_binding ( & mut self , import : Import < ' ra > , is_indeterminate : bool ) {
488
496
if let ImportKind :: Single { target, ref bindings, .. } = import. kind {
489
497
if !( is_indeterminate
490
- || bindings. iter ( ) . all ( |binding| {
491
- matches ! ( binding. get( ) , Progress :: Pending | Progress :: Ready ( None ) )
492
- } ) )
498
+ || bindings. iter ( ) . all ( |binding| binding. get ( ) . binding ( ) . is_none ( ) ) )
493
499
{
494
500
return ; // Has resolution, do not create the dummy binding
495
501
}
@@ -567,7 +573,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
567
573
if let ImportKind :: Single { source, ref bindings, .. } = import. kind
568
574
&& source. name == kw:: SelfLower
569
575
// Silence `unresolved import` error if E0429 is already emitted
570
- && let Progress :: Ready ( None ) = bindings. value_ns . get ( )
576
+ && let PendingBinding :: Ready ( None ) = bindings. value_ns . get ( )
571
577
{
572
578
continue ;
573
579
}
@@ -830,7 +836,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
830
836
let mut indeterminate_count = 0 ;
831
837
self . per_ns ( |this, ns| {
832
838
if !type_ns_only || ns == TypeNS {
833
- if bindings[ ns] . get ( ) != Progress :: Pending {
839
+ if bindings[ ns] . get ( ) != PendingBinding :: Pending {
834
840
return ;
835
841
} ;
836
842
let binding_result = this. maybe_resolve_ident_in_module (
@@ -857,7 +863,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
857
863
// We need the `target`, `source` can be extracted.
858
864
let imported_binding = this. import ( binding, import) ;
859
865
this. define ( parent, target, ns, imported_binding) ;
860
- Poll :: Ready ( Some ( imported_binding) )
866
+ PendingBinding :: Ready ( Some ( imported_binding) )
861
867
}
862
868
Err ( Determinacy :: Determined ) => {
863
869
// Don't update the resolution for underscores, because it was never added.
@@ -867,11 +873,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
867
873
resolution. single_imports . swap_remove ( & import) ;
868
874
} ) ;
869
875
}
870
- Poll :: Ready ( None )
876
+ PendingBinding :: Ready ( None )
871
877
}
872
878
Err ( Determinacy :: Undetermined ) => {
873
879
indeterminate_count += 1 ;
874
- Poll :: Pending
880
+ PendingBinding :: Pending
875
881
}
876
882
} ;
877
883
bindings[ ns] . set ( binding) ;
@@ -887,10 +893,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
887
893
/// consolidate multiple unresolved import errors into a single diagnostic.
888
894
fn finalize_import ( & mut self , import : Import < ' ra > ) -> Option < UnresolvedImportError > {
889
895
let ignore_binding = if let ImportKind :: Single { bindings, .. } = & import. kind {
890
- match bindings[ TypeNS ] . get ( ) {
891
- Poll :: Ready ( binding) => binding,
892
- _ => None ,
893
- }
896
+ bindings[ TypeNS ] . get ( ) . binding ( )
894
897
} else {
895
898
None
896
899
} ;
@@ -1080,42 +1083,34 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
1080
1083
let mut all_ns_err = true ;
1081
1084
self . per_ns ( |this, ns| {
1082
1085
if !type_ns_only || ns == TypeNS {
1083
- let ignore_binding = match bindings[ ns] . get ( ) {
1084
- Poll :: Ready ( binding) => binding,
1085
- _ => None ,
1086
- } ;
1087
1086
let binding = this. resolve_ident_in_module (
1088
1087
module,
1089
1088
ident,
1090
1089
ns,
1091
1090
& import. parent_scope ,
1092
1091
Some ( Finalize { report_private : false , ..finalize } ) ,
1093
- ignore_binding ,
1092
+ bindings [ ns ] . get ( ) . binding ( ) ,
1094
1093
Some ( import) ,
1095
1094
) ;
1096
1095
1097
1096
match binding {
1098
1097
Ok ( binding) => {
1099
1098
// Consistency checks, analogous to `finalize_macro_resolutions`.
1100
- let initial_res = bindings[ ns] . get ( ) . map ( |maybe_binding| {
1101
- maybe_binding. map ( |binding| binding. import_source ( ) ) . map (
1102
- |initial_binding| {
1103
- all_ns_err = false ;
1104
- if let Progress :: Ready ( Some ( binding) ) = bindings[ ns] . get ( )
1105
- && target. name == kw:: Underscore
1106
- && initial_binding. is_extern_crate ( )
1107
- && !initial_binding. is_import ( )
1108
- {
1109
- let used = if import. module_path . is_empty ( ) {
1110
- Used :: Scope
1111
- } else {
1112
- Used :: Other
1113
- } ;
1114
- this. record_use ( ident, binding, used) ;
1115
- }
1116
- binding. res ( )
1117
- } ,
1118
- )
1099
+ let initial_res = bindings[ ns] . get ( ) . binding ( ) . map ( |binding| {
1100
+ let initial_binding = binding. import_source ( ) ;
1101
+ all_ns_err = false ;
1102
+ if target. name == kw:: Underscore
1103
+ && initial_binding. is_extern_crate ( )
1104
+ && !initial_binding. is_import ( )
1105
+ {
1106
+ let used = if import. module_path . is_empty ( ) {
1107
+ Used :: Scope
1108
+ } else {
1109
+ Used :: Other
1110
+ } ;
1111
+ this. record_use ( ident, binding, used) ;
1112
+ }
1113
+ initial_binding. res ( )
1119
1114
} ) ;
1120
1115
let res = binding. res ( ) ;
1121
1116
let has_ambiguity_error =
@@ -1125,7 +1120,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
1125
1120
. span_delayed_bug ( import. span , "some error happened for an import" ) ;
1126
1121
return ;
1127
1122
}
1128
- if let Progress :: Ready ( Some ( initial_res) ) = initial_res {
1123
+ if let Some ( initial_res) = initial_res {
1129
1124
if res != initial_res {
1130
1125
span_bug ! ( import. span, "inconsistent resolution for an import" ) ;
1131
1126
}
@@ -1268,11 +1263,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
1268
1263
let mut any_successful_reexport = false ;
1269
1264
let mut crate_private_reexport = false ;
1270
1265
self . per_ns ( |this, ns| {
1271
- let binding = match bindings[ ns] . get ( ) {
1272
- Poll :: Ready ( Some ( binding) ) => Some ( binding. import_source ( ) ) ,
1273
- _ => None ,
1274
- } ;
1275
- let Some ( binding) = binding else {
1266
+ let Some ( binding) = bindings[ ns] . get ( ) . binding ( ) . map ( |b| b. import_source ( ) ) else {
1276
1267
return ;
1277
1268
} ;
1278
1269
@@ -1343,11 +1334,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
1343
1334
let mut full_path = import. module_path . clone ( ) ;
1344
1335
full_path. push ( Segment :: from_ident ( ident) ) ;
1345
1336
self . per_ns ( |this, ns| {
1346
- let binding = match bindings[ ns] . get ( ) {
1347
- Poll :: Ready ( Some ( binding) ) => Some ( binding. import_source ( ) ) ,
1348
- _ => None ,
1349
- } ;
1350
- if let Some ( binding) = binding {
1337
+ if let Some ( binding) = bindings[ ns] . get ( ) . binding ( ) . map ( |b| b. import_source ( ) ) {
1351
1338
this. lint_if_path_starts_with_module ( Some ( finalize) , & full_path, Some ( binding) ) ;
1352
1339
}
1353
1340
} ) ;
@@ -1357,11 +1344,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
1357
1344
// this may resolve to either a value or a type, but for documentation
1358
1345
// purposes it's good enough to just favor one over the other.
1359
1346
self . per_ns ( |this, ns| {
1360
- let binding = match bindings[ ns] . get ( ) {
1361
- Poll :: Ready ( Some ( binding) ) => Some ( binding. import_source ( ) ) ,
1362
- _ => None ,
1363
- } ;
1364
- if let Some ( binding) = binding {
1347
+ if let Some ( binding) = bindings[ ns] . get ( ) . binding ( ) . map ( |b| b. import_source ( ) ) {
1365
1348
this. import_res_map . entry ( import_id) . or_default ( ) [ ns] = Some ( binding. res ( ) ) ;
1366
1349
}
1367
1350
} ) ;
@@ -1399,26 +1382,19 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
1399
1382
let mut is_redundant = true ;
1400
1383
let mut redundant_span = PerNS { value_ns : None , type_ns : None , macro_ns : None } ;
1401
1384
self . per_ns ( |this, ns| {
1402
- let binding = match bindings[ ns] . get ( ) {
1403
- Poll :: Ready ( Some ( binding) ) => Some ( binding. import_source ( ) ) ,
1404
- _ => None ,
1405
- } ;
1385
+ let binding = bindings[ ns] . get ( ) . binding ( ) . map ( |b| b. import_source ( ) ) ;
1406
1386
if is_redundant && let Some ( binding) = binding {
1407
1387
if binding. res ( ) == Res :: Err {
1408
1388
return ;
1409
1389
}
1410
1390
1411
- let ignore_binding = match bindings[ ns] . get ( ) {
1412
- Poll :: Ready ( binding) => binding,
1413
- _ => None ,
1414
- } ;
1415
1391
match this. early_resolve_ident_in_lexical_scope (
1416
1392
target,
1417
1393
ScopeSet :: All ( ns) ,
1418
1394
& import. parent_scope ,
1419
1395
None ,
1420
1396
false ,
1421
- ignore_binding ,
1397
+ bindings [ ns ] . get ( ) . binding ( ) ,
1422
1398
None ,
1423
1399
) {
1424
1400
Ok ( other_binding) => {
0 commit comments