@@ -19,39 +19,53 @@ use std::str;
19
19
/// Collected spans during parsing for places where a certain feature was
20
20
/// used and should be feature gated accordingly in `check_crate`.
21
21
#[ derive( Default ) ]
22
- crate struct GatedSpans {
23
- /// Spans collected for gating `let_chains`, e.g. `if a && let b = c {}`.
24
- crate let_chains : Lock < Vec < Span > > ,
25
- /// Spans collected for gating `async_closure`, e.g. `async || ..`.
26
- crate async_closure : Lock < Vec < Span > > ,
27
- /// Spans collected for gating `yield e?` expressions (`generators` gate).
28
- crate yields : Lock < Vec < Span > > ,
29
- /// Spans collected for gating `or_patterns`, e.g. `Some(Foo | Bar)`.
30
- crate or_patterns : Lock < Vec < Span > > ,
31
- /// Spans collected for gating `const_extern_fn`, e.g. `const extern fn foo`.
32
- crate const_extern_fn : Lock < Vec < Span > > ,
33
- /// Spans collected for gating `trait_alias`, e.g. `trait Foo = Ord + Eq;`.
34
- pub trait_alias : Lock < Vec < Span > > ,
35
- /// Spans collected for gating `associated_type_bounds`, e.g. `Iterator<Item: Ord>`.
36
- pub associated_type_bounds : Lock < Vec < Span > > ,
37
- /// Spans collected for gating `crate_visibility_modifier`, e.g. `crate fn`.
38
- pub crate_visibility_modifier : Lock < Vec < Span > > ,
39
- /// Spans collected for gating `const_generics`, e.g. `const N: usize`.
40
- pub const_generics : Lock < Vec < Span > > ,
41
- /// Spans collected for gating `decl_macro`, e.g. `macro m() {}`.
42
- pub decl_macro : Lock < Vec < Span > > ,
43
- /// Spans collected for gating `box_patterns`, e.g. `box 0`.
44
- pub box_patterns : Lock < Vec < Span > > ,
45
- /// Spans collected for gating `exclusive_range_pattern`, e.g. `0..2`.
46
- pub exclusive_range_pattern : Lock < Vec < Span > > ,
47
- /// Spans collected for gating `try_blocks`, e.g. `try { a? + b? }`.
48
- pub try_blocks : Lock < Vec < Span > > ,
49
- /// Spans collected for gating `label_break_value`, e.g. `'label: { ... }`.
50
- pub label_break_value : Lock < Vec < Span > > ,
51
- /// Spans collected for gating `box_syntax`, e.g. `box $expr`.
52
- pub box_syntax : Lock < Vec < Span > > ,
53
- /// Spans collected for gating `type_ascription`, e.g. `42: usize`.
54
- pub type_ascription : Lock < Vec < Span > > ,
22
+ pub struct GatedSpans {
23
+ pub spans : Lock < FxHashMap < Symbol , Vec < Span > > > ,
24
+ }
25
+
26
+ impl GatedSpans {
27
+ /// Feature gate the given `span` under the given `feature`
28
+ /// which is same `Symbol` used in `active.rs`.
29
+ pub fn gate ( & self , feature : Symbol , span : Span ) {
30
+ self . spans
31
+ . borrow_mut ( )
32
+ . entry ( feature)
33
+ . or_default ( )
34
+ . push ( span) ;
35
+ }
36
+
37
+ /// Ungate the last span under the given `feature`.
38
+ /// Panics if the given `span` wasn't the last one.
39
+ ///
40
+ /// Using this is discouraged unless you have a really good reason to.
41
+ pub fn ungate_last ( & self , feature : Symbol , span : Span ) {
42
+ let removed_span = self . spans
43
+ . borrow_mut ( )
44
+ . entry ( feature)
45
+ . or_default ( )
46
+ . pop ( )
47
+ . unwrap ( ) ;
48
+ debug_assert_eq ! ( span, removed_span) ;
49
+ }
50
+
51
+ /// Is the provided `feature` gate ungated currently?
52
+ ///
53
+ /// Using this is discouraged unless you have a really good reason to.
54
+ pub fn is_ungated ( & self , feature : Symbol ) -> bool {
55
+ self . spans
56
+ . borrow ( )
57
+ . get ( & feature)
58
+ . map_or ( true , |spans| spans. is_empty ( ) )
59
+ }
60
+
61
+ /// Prepend the given set of `spans` onto the set in `self`.
62
+ pub fn merge ( & self , mut spans : FxHashMap < Symbol , Vec < Span > > ) {
63
+ let mut inner = self . spans . borrow_mut ( ) ;
64
+ for ( gate, mut gate_spans) in inner. drain ( ) {
65
+ spans. entry ( gate) . or_default ( ) . append ( & mut gate_spans) ;
66
+ }
67
+ * inner = spans;
68
+ }
55
69
}
56
70
57
71
/// Info about a parsing session.
@@ -72,7 +86,7 @@ pub struct ParseSess {
72
86
/// analysis.
73
87
pub ambiguous_block_expr_parse : Lock < FxHashMap < Span , Span > > ,
74
88
pub injected_crate_name : Once < Symbol > ,
75
- crate gated_spans : GatedSpans ,
89
+ pub gated_spans : GatedSpans ,
76
90
/// The parser has reached `Eof` due to an unclosed brace. Used to silence unnecessary errors.
77
91
pub reached_eof : Lock < bool > ,
78
92
}
0 commit comments