@@ -26,6 +26,7 @@ use rustc_target::spec::{PanicStrategy, RelocModel, SanitizerSet};
2626use rustc_target:: spec:: { Target , TargetTriple , TARGETS } ;
2727
2828use crate :: config:: CrateType ;
29+ use crate :: errors:: DisallowConfig ;
2930use crate :: Session ;
3031
3132use std:: hash:: Hash ;
@@ -379,3 +380,54 @@ impl CheckCfg {
379380 ins ! ( sym:: windows, no_values) ;
380381 }
381382}
383+
384+ pub ( crate ) fn disallow_cfgs ( sess : & Session , user_cfgs : & Cfg ) {
385+ let disallow = |cfg : & ( Symbol , Option < Symbol > ) , controlled_by| {
386+ let cfg_name = cfg. 0 ;
387+ let cfg = if let Some ( value) = cfg. 1 {
388+ format ! ( r#"{}="{}""# , cfg_name, value)
389+ } else {
390+ format ! ( "{}" , cfg_name)
391+ } ;
392+ sess. dcx ( ) . emit_fatal ( DisallowConfig { cfg, cfg_name, controlled_by } )
393+ } ;
394+
395+ // We want to restrict setting cfgs that will produce "incoherent" behavior between
396+ // the cfg and the "real" flag that sets it, but not all cfgs produce incoherent
397+ // behavior, we therefore exclude those cfgs:
398+ //
399+ // - test
400+ // - clippy
401+ // - doc
402+ // - doctest
403+ // - miri
404+ // - rustfmt
405+ // - overflow_checks
406+ // - debug_assertions
407+ // - ub_checks
408+
409+ for cfg in user_cfgs {
410+ match cfg {
411+ ( sym:: proc_macro, None ) => disallow ( cfg, "--crate-type proc-macro" ) ,
412+ ( sym:: panic, Some ( sym:: abort) ) => disallow ( cfg, "-C panic" ) ,
413+ ( sym:: panic, Some ( sym:: unwind) ) => disallow ( cfg, "-C panic" ) ,
414+ ( sym:: target_feature, Some ( _) ) => disallow ( cfg, "-C target-feature" ) ,
415+ ( sym:: unix, None )
416+ | ( sym:: windows, None )
417+ | ( sym:: relocation_model, Some ( _) )
418+ | ( sym:: target_abi, None | Some ( _) )
419+ | ( sym:: target_arch, Some ( _) )
420+ | ( sym:: target_endian, Some ( _) )
421+ | ( sym:: target_env, None | Some ( _) )
422+ | ( sym:: target_family, Some ( _) )
423+ | ( sym:: target_os, Some ( _) )
424+ | ( sym:: target_pointer_width, Some ( _) )
425+ | ( sym:: target_vendor, None | Some ( _) )
426+ | ( sym:: target_has_atomic, Some ( _) )
427+ | ( sym:: target_has_atomic_equal_alignment, Some ( _) )
428+ | ( sym:: target_has_atomic_load_store, Some ( _) )
429+ | ( sym:: target_thread_local, None ) => disallow ( cfg, "--target" ) ,
430+ _ => { }
431+ }
432+ }
433+ }
0 commit comments