11use std:: collections:: { BTreeMap , BTreeSet , HashMap , HashSet } ;
22use std:: path:: { Path , PathBuf } ;
33use std:: sync:: Arc ;
4- use std:: { env, fs} ;
4+ use std:: { env, fmt , fs} ;
55
66use crate :: core:: compiler:: { CompileKind , DefaultExecutor , Executor , UnitOutput } ;
77use crate :: core:: { Dependency , Edition , Package , PackageId , SourceId , Target , Workspace } ;
@@ -14,6 +14,7 @@ use crate::util::errors::CargoResult;
1414use crate :: util:: { Filesystem , GlobalContext , Rustc } ;
1515use crate :: { drop_println, ops} ;
1616
17+ use annotate_snippets:: Level ;
1718use anyhow:: { Context as _, bail} ;
1819use cargo_util:: paths;
1920use cargo_util_schemas:: core:: PartialVersion ;
@@ -39,6 +40,42 @@ impl Drop for Transaction {
3940 }
4041}
4142
43+ enum RustupToolchainSource {
44+ Default ,
45+ Environment ,
46+ CommandLine ,
47+ OverrideDB ,
48+ ToolchainFile ,
49+ Other ( String ) ,
50+ }
51+
52+ #[ allow( dead_code) ]
53+ impl RustupToolchainSource {
54+ fn is_implicit_override ( & self ) -> bool {
55+ match self {
56+ Self :: Default => false ,
57+ Self :: Environment => true ,
58+ Self :: CommandLine => false ,
59+ Self :: OverrideDB => true ,
60+ Self :: ToolchainFile => true ,
61+ Self :: Other ( _) => true ,
62+ }
63+ }
64+ }
65+
66+ impl fmt:: Display for RustupToolchainSource {
67+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
68+ f. write_str ( match self {
69+ Self :: Default => "default" ,
70+ Self :: Environment => "environment variable" ,
71+ Self :: CommandLine => "command line" ,
72+ Self :: OverrideDB => "rustup directory override" ,
73+ Self :: ToolchainFile => "rustup toolchain file" ,
74+ Self :: Other ( other) => other,
75+ } )
76+ }
77+ }
78+
4279struct InstallablePackage < ' gctx > {
4380 gctx : & ' gctx GlobalContext ,
4481 opts : ops:: CompileOptions ,
@@ -316,6 +353,28 @@ impl<'gctx> InstallablePackage<'gctx> {
316353 fn install_one ( mut self , dry_run : bool ) -> CargoResult < bool > {
317354 self . gctx . shell ( ) . status ( "Installing" , & self . pkg ) ?;
318355
356+ if let Some ( source) = get_rustup_toolchain_source ( )
357+ && source. is_implicit_override ( )
358+ {
359+ #[ expect( clippy:: disallowed_methods, reason = "consistency with rustup" ) ]
360+ let maybe_toolchain = env:: var ( "RUSTUP_TOOLCHAIN" )
361+ . ok ( )
362+ . map ( |toolchain| format ! ( " with `{toolchain}`" ) )
363+ . unwrap_or_default ( ) ;
364+ let report = & [ Level :: WARNING
365+ . secondary_title ( format ! (
366+ "default toolchain implicitly overridden{maybe_toolchain} by {source}"
367+ ) )
368+ . element ( Level :: HELP . message ( format ! (
369+ "use `cargo +stable install` if you meant to use the stable toolchain"
370+ ) ) )
371+ . element ( Level :: NOTE . message ( format ! (
372+ "rustup selects the toolchain based on the parent environment and not the \
373+ environment of the package being installed"
374+ ) ) ) ] ;
375+ self . gctx . shell ( ) . print_report ( report, false ) ?;
376+ }
377+
319378 // Normalize to absolute path for consistency throughout.
320379 // See: https://github.com/rust-lang/cargo/issues/16023
321380 let dst = self . root . join ( "bin" ) . into_path_unlocked ( ) ;
@@ -611,6 +670,20 @@ impl<'gctx> InstallablePackage<'gctx> {
611670 }
612671}
613672
673+ fn get_rustup_toolchain_source ( ) -> Option < RustupToolchainSource > {
674+ #[ expect( clippy:: disallowed_methods, reason = "consistency with rustup" ) ]
675+ let source = std:: env:: var ( "RUSTUP_TOOLCHAIN_SOURCE" ) . ok ( ) ?;
676+ let source = match source. as_str ( ) {
677+ "default" => RustupToolchainSource :: Default ,
678+ "env" => RustupToolchainSource :: Environment ,
679+ "cli" => RustupToolchainSource :: CommandLine ,
680+ "path-override" => RustupToolchainSource :: OverrideDB ,
681+ "toolchain-file" => RustupToolchainSource :: ToolchainFile ,
682+ other => RustupToolchainSource :: Other ( other. to_owned ( ) ) ,
683+ } ;
684+ Some ( source)
685+ }
686+
614687fn make_warning_about_missing_features ( binaries : & [ & Target ] ) -> String {
615688 let max_targets_listed = 7 ;
616689 let target_features_message = binaries
0 commit comments