@@ -10,10 +10,9 @@ use rustc_session::config::ExpectedValues;
10
10
use rustc_session:: lint:: builtin:: UNEXPECTED_CFGS ;
11
11
use rustc_session:: lint:: BuiltinLintDiagnostics ;
12
12
use rustc_session:: parse:: { feature_err, ParseSess } ;
13
- use rustc_session:: Session ;
13
+ use rustc_session:: { RustcVersion , Session } ;
14
14
use rustc_span:: hygiene:: Transparency ;
15
15
use rustc_span:: { symbol:: sym, symbol:: Symbol , Span } ;
16
- use std:: fmt:: { self , Display } ;
17
16
use std:: num:: NonZeroU32 ;
18
17
19
18
use crate :: session_diagnostics:: { self , IncorrectReprFormatGenericCause } ;
@@ -24,8 +23,6 @@ use crate::session_diagnostics::{self, IncorrectReprFormatGenericCause};
24
23
/// For more, see [this pull request](https://github.com/rust-lang/rust/pull/100591).
25
24
pub const VERSION_PLACEHOLDER : & str = "CURRENT_RUSTC_VERSION" ;
26
25
27
- pub const CURRENT_RUSTC_VERSION : & str = env ! ( "CFG_RELEASE" ) ;
28
-
29
26
pub fn is_builtin_attr ( attr : & Attribute ) -> bool {
30
27
attr. is_doc_comment ( ) || attr. ident ( ) . is_some_and ( |ident| is_builtin_attr_name ( ident. name ) )
31
28
}
@@ -153,7 +150,7 @@ pub enum StabilityLevel {
153
150
#[ derive( Encodable , Decodable , PartialEq , Copy , Clone , Debug , Eq , Hash ) ]
154
151
#[ derive( HashStable_Generic ) ]
155
152
pub enum Since {
156
- Version ( Version ) ,
153
+ Version ( RustcVersion ) ,
157
154
/// Stabilized in the upcoming version, whatever number that is.
158
155
Current ,
159
156
/// Failed to parse a stabilization version.
@@ -382,7 +379,7 @@ fn parse_stability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabilit
382
379
let since = if let Some ( since) = since {
383
380
if since. as_str ( ) == VERSION_PLACEHOLDER {
384
381
Since :: Current
385
- } else if let Some ( version) = parse_version ( since. as_str ( ) , false ) {
382
+ } else if let Some ( version) = parse_version ( since) {
386
383
Since :: Version ( version)
387
384
} else {
388
385
sess. emit_err ( session_diagnostics:: InvalidSince { span : attr. span } ) ;
@@ -567,31 +564,20 @@ fn gate_cfg(gated_cfg: &GatedCfg, cfg_span: Span, sess: &ParseSess, features: &F
567
564
}
568
565
}
569
566
570
- #[ derive( Encodable , Decodable , Copy , Clone , Debug , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
571
- #[ derive( HashStable_Generic ) ]
572
- pub struct Version {
573
- pub major : u16 ,
574
- pub minor : u16 ,
575
- pub patch : u16 ,
576
- }
577
-
578
- fn parse_version ( s : & str , allow_appendix : bool ) -> Option < Version > {
579
- let mut components = s. split ( '-' ) ;
567
+ /// Parse a rustc version number written inside string literal in an attribute,
568
+ /// like appears in `since = "1.0.0"`. Suffixes like "-dev" and "-nightly" are
569
+ /// not accepted in this position, unlike when parsing CFG_RELEASE.
570
+ fn parse_version ( s : Symbol ) -> Option < RustcVersion > {
571
+ let mut components = s. as_str ( ) . split ( '-' ) ;
580
572
let d = components. next ( ) ?;
581
- if !allow_appendix && components. next ( ) . is_some ( ) {
573
+ if components. next ( ) . is_some ( ) {
582
574
return None ;
583
575
}
584
576
let mut digits = d. splitn ( 3 , '.' ) ;
585
577
let major = digits. next ( ) ?. parse ( ) . ok ( ) ?;
586
578
let minor = digits. next ( ) ?. parse ( ) . ok ( ) ?;
587
579
let patch = digits. next ( ) . unwrap_or ( "0" ) . parse ( ) . ok ( ) ?;
588
- Some ( Version { major, minor, patch } )
589
- }
590
-
591
- impl Display for Version {
592
- fn fmt ( & self , formatter : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
593
- write ! ( formatter, "{}.{}.{}" , self . major, self . minor, self . patch)
594
- }
580
+ Some ( RustcVersion { major, minor, patch } )
595
581
}
596
582
597
583
/// Evaluate a cfg-like condition (with `any` and `all`), using `eval` to
@@ -623,17 +609,16 @@ pub fn eval_condition(
623
609
return false ;
624
610
}
625
611
} ;
626
- let Some ( min_version) = parse_version ( min_version. as_str ( ) , false ) else {
612
+ let Some ( min_version) = parse_version ( * min_version) else {
627
613
sess. emit_warning ( session_diagnostics:: UnknownVersionLiteral { span : * span } ) ;
628
614
return false ;
629
615
} ;
630
- let rustc_version = parse_version ( CURRENT_RUSTC_VERSION , true ) . unwrap ( ) ;
631
616
632
617
// See https://github.com/rust-lang/rust/issues/64796#issuecomment-640851454 for details
633
618
if sess. assume_incomplete_release {
634
- rustc_version > min_version
619
+ RustcVersion :: CURRENT > min_version
635
620
} else {
636
- rustc_version >= min_version
621
+ RustcVersion :: CURRENT >= min_version
637
622
}
638
623
}
639
624
ast:: MetaItemKind :: List ( mis) => {
0 commit comments