@@ -24,6 +24,7 @@ pub use crate::flags::Subcommand;
2424use crate :: flags:: { Color , Flags , Warnings } ;
2525use crate :: util:: { exe, output, t} ;
2626use once_cell:: sync:: OnceCell ;
27+ use semver:: Version ;
2728use serde:: { Deserialize , Deserializer } ;
2829use serde_derive:: Deserialize ;
2930
@@ -1019,6 +1020,8 @@ impl Config {
10191020 config. download_beta_toolchain ( ) ;
10201021 config. out . join ( config. build . triple ) . join ( "stage0/bin/rustc" )
10211022 } ) ;
1023+ config. check_build_rustc_version ( ) ;
1024+
10221025 config. initial_cargo = build
10231026 . cargo
10241027 . map ( |cargo| {
@@ -1680,6 +1683,37 @@ impl Config {
16801683 self . rust_codegen_backends . get ( 0 ) . cloned ( )
16811684 }
16821685
1686+ fn check_build_rustc_version ( & self ) {
1687+ // check rustc version is same or lower with 1 apart from the building one
1688+ let mut cmd = Command :: new ( & self . initial_rustc ) ;
1689+ cmd. arg ( "--version" ) ;
1690+ let rustc_output = output ( & mut cmd)
1691+ . lines ( )
1692+ . next ( )
1693+ . unwrap ( )
1694+ . split ( ' ' )
1695+ . nth ( 1 )
1696+ . unwrap ( )
1697+ . split ( '-' )
1698+ . next ( )
1699+ . unwrap ( )
1700+ . to_owned ( ) ;
1701+ let rustc_version = Version :: parse ( & rustc_output. trim ( ) ) . unwrap ( ) ;
1702+ let source_version =
1703+ Version :: parse ( & fs:: read_to_string ( self . src . join ( "src/version" ) ) . unwrap ( ) . trim ( ) )
1704+ . unwrap ( ) ;
1705+ if !( source_version == rustc_version
1706+ || ( source_version. major == rustc_version. major
1707+ && source_version. minor == rustc_version. minor + 1 ) )
1708+ {
1709+ let prev_version = format ! ( "{}.{}.x" , source_version. major, source_version. minor - 1 ) ;
1710+ panic ! (
1711+ "Unexpected rustc version: {}, we should use {}/{} to build source with {}" ,
1712+ rustc_version, prev_version, source_version, source_version
1713+ ) ;
1714+ }
1715+ }
1716+
16831717 /// Returns the commit to download, or `None` if we shouldn't download CI artifacts.
16841718 fn download_ci_rustc_commit ( & self , download_rustc : Option < StringOrBool > ) -> Option < String > {
16851719 // If `download-rustc` is not set, default to rebuilding.
0 commit comments