Skip to content

Commit 53e9d43

Browse files
committed
-Zunsigned-char=unsigned|signed|default flag for c_char->u8/i8 selection override
1 parent 2b4694a commit 53e9d43

24 files changed

+175
-53
lines changed

compiler/rustc_session/src/config.rs

+37-8
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use rustc_macros::{Decodable, Encodable, HashStable_Generic};
2323
use rustc_span::edition::{DEFAULT_EDITION, EDITION_NAME_LIST, Edition, LATEST_STABLE_EDITION};
2424
use rustc_span::source_map::FilePathMapping;
2525
use rustc_span::{
26-
FileName, FileNameDisplayPreference, RealFileName, SourceFileHashAlgorithm, Symbol, sym,
26+
FileName, FileNameDisplayPreference, RealFileName, SourceFileHashAlgorithm, Symbol, kw, sym,
2727
};
2828
use rustc_target::spec::{
2929
FramePointer, LinkSelfContainedComponents, LinkerFeatures, SplitDebuginfo, Target, TargetTuple,
@@ -2931,13 +2931,13 @@ pub(crate) mod dep_tracking {
29312931
};
29322932

29332933
use super::{
2934-
AutoDiff, BranchProtection, CFGuard, CFProtection, CollapseMacroDebuginfo, CoverageOptions,
2935-
CrateType, DebugInfo, DebugInfoCompression, ErrorOutputType, FmtDebug, FunctionReturn,
2936-
InliningThreshold, InstrumentCoverage, InstrumentXRay, LinkerPluginLto, LocationDetail,
2937-
LtoCli, MirStripDebugInfo, NextSolverConfig, OomStrategy, OptLevel, OutFileName,
2938-
OutputType, OutputTypes, PatchableFunctionEntry, Polonius, RemapPathScopeComponents,
2939-
ResolveDocLinks, SourceFileHashAlgorithm, SplitDwarfKind, SwitchWithOptPath,
2940-
SymbolManglingVersion, WasiExecModel,
2934+
AutoDiff, BranchProtection, CCharType, CFGuard, CFProtection, CollapseMacroDebuginfo,
2935+
CoverageOptions, CrateType, DebugInfo, DebugInfoCompression, ErrorOutputType, FmtDebug,
2936+
FunctionReturn, InliningThreshold, InstrumentCoverage, InstrumentXRay, LinkerPluginLto,
2937+
LocationDetail, LtoCli, MirStripDebugInfo, NextSolverConfig, OomStrategy, OptLevel,
2938+
OutFileName, OutputType, OutputTypes, PatchableFunctionEntry, Polonius,
2939+
RemapPathScopeComponents, ResolveDocLinks, SourceFileHashAlgorithm, SplitDwarfKind,
2940+
SwitchWithOptPath, SymbolManglingVersion, WasiExecModel,
29412941
};
29422942
use crate::lint;
29432943
use crate::utils::NativeLib;
@@ -3040,6 +3040,7 @@ pub(crate) mod dep_tracking {
30403040
FunctionReturn,
30413041
WasmCAbi,
30423042
Align,
3043+
CCharType,
30433044
);
30443045

30453046
impl<T1, T2> DepTrackingHash for (T1, T2)
@@ -3314,3 +3315,31 @@ impl MirIncludeSpans {
33143315
self == MirIncludeSpans::On
33153316
}
33163317
}
3318+
3319+
/// The different settings that the `-Zc-char-type` flag can have.
3320+
#[derive(Clone, Copy, PartialEq, Hash, Debug, Default)]
3321+
pub enum CCharType {
3322+
/// Use default signed/unsigned c_char according to target configuration
3323+
#[default]
3324+
Default,
3325+
3326+
/// Set c_char to signed i8
3327+
Signed,
3328+
3329+
/// Set c_char to unsigned u8
3330+
Unsigned,
3331+
}
3332+
3333+
impl CCharType {
3334+
pub const fn desc_symbol(&self) -> Symbol {
3335+
match *self {
3336+
Self::Default => kw::Default,
3337+
Self::Signed => sym::signed,
3338+
Self::Unsigned => sym::unsigned,
3339+
}
3340+
}
3341+
3342+
pub const fn all() -> [Symbol; 3] {
3343+
[Self::Unsigned.desc_symbol(), Self::Signed.desc_symbol(), Self::Default.desc_symbol()]
3344+
}
3345+
}

compiler/rustc_session/src/config/cfg.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use rustc_span::{Symbol, sym};
3232
use rustc_target::spec::{PanicStrategy, RelocModel, SanitizerSet, Target};
3333

3434
use crate::Session;
35-
use crate::config::{CrateType, FmtDebug};
35+
use crate::config::{CCharType, CrateType, FmtDebug};
3636

3737
/// The parsed `--cfg` options that define the compilation environment of the
3838
/// crate, used to drive conditional compilation.
@@ -144,6 +144,7 @@ pub(crate) fn disallow_cfgs(sess: &Session, user_cfgs: &Cfg) {
144144
| (sym::target_has_atomic_load_store, Some(_))
145145
| (sym::target_thread_local, None) => disallow(cfg, "--target"),
146146
(sym::fmt_debug, None | Some(_)) => disallow(cfg, "-Z fmt-debug"),
147+
(sym::c_char_type, None | Some(_)) => disallow(cfg, "-Z c-char-type"),
147148
(sym::emscripten_wasm_eh, None | Some(_)) => disallow(cfg, "-Z emscripten_wasm_eh"),
148149
_ => {}
149150
}
@@ -306,6 +307,8 @@ pub(crate) fn default_configuration(sess: &Session) -> Cfg {
306307
ins_none!(sym::contract_checks);
307308
}
308309

310+
ins_sym!(sym::c_char_type, sess.opts.unstable_opts.c_char_type.desc_symbol());
311+
309312
ret
310313
}
311314

@@ -470,5 +473,7 @@ impl CheckCfg {
470473

471474
ins!(sym::unix, no_values);
472475
ins!(sym::windows, no_values);
476+
477+
ins!(sym::c_char_type, empty_values).extend(CCharType::all());
473478
}
474479
}

compiler/rustc_session/src/options.rs

+13
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,7 @@ mod desc {
794794
pub(crate) const parse_mir_include_spans: &str =
795795
"either a boolean (`yes`, `no`, `on`, `off`, etc), or `nll` (default: `nll`)";
796796
pub(crate) const parse_align: &str = "a number that is a power of 2 between 1 and 2^29";
797+
pub(crate) const parse_c_char_type: &str = "one of `default`, `unsigned`, `signed`";
797798
}
798799

799800
pub mod parse {
@@ -954,6 +955,16 @@ pub mod parse {
954955
true
955956
}
956957

958+
pub(crate) fn parse_c_char_type(slot: &mut CCharType, v: Option<&str>) -> bool {
959+
*slot = match v {
960+
Some("unsigned") => CCharType::Unsigned,
961+
Some("signed") => CCharType::Signed,
962+
Some("default") => CCharType::Default,
963+
_ => return false,
964+
};
965+
true
966+
}
967+
957968
pub(crate) fn parse_location_detail(ld: &mut LocationDetail, v: Option<&str>) -> bool {
958969
if let Some(v) = v {
959970
ld.line = false;
@@ -2099,6 +2110,8 @@ options! {
20992110
"emit noalias metadata for box (default: yes)"),
21002111
branch_protection: Option<BranchProtection> = (None, parse_branch_protection, [TRACKED],
21012112
"set options for branch target identification and pointer authentication on AArch64"),
2113+
c_char_type: CCharType = (CCharType::default(), parse_c_char_type, [TRACKED TARGET_MODIFIER],
2114+
"Make c_char type unsigned [default|signed|unsigned]"),
21022115
cf_protection: CFProtection = (CFProtection::None, parse_cfprotection, [TRACKED],
21032116
"instrument control-flow architecture protection"),
21042117
check_cfg_all_expected: bool = (false, parse_bool, [UNTRACKED],

compiler/rustc_span/src/symbol.rs

+3
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,7 @@ symbols! {
557557
btreeset_iter,
558558
builtin_syntax,
559559
c,
560+
c_char_type,
560561
c_dash_variadic,
561562
c_str,
562563
c_str_literals,
@@ -1855,6 +1856,7 @@ symbols! {
18551856
shr_assign,
18561857
sig_dfl,
18571858
sig_ign,
1859+
signed,
18581860
simd,
18591861
simd_add,
18601862
simd_and,
@@ -2169,6 +2171,7 @@ symbols! {
21692171
unsafe_fields,
21702172
unsafe_no_drop_flag,
21712173
unsafe_pin_internals,
2174+
unsigned,
21722175
unsize,
21732176
unsized_const_param_ty,
21742177
unsized_const_params,

library/core/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,5 @@ check-cfg = [
3838
# and to stdarch `core_arch` crate which messes-up with Cargo list
3939
# of declared features, we therefor expect any feature cfg
4040
'cfg(feature, values(any()))',
41+
'cfg(c_char_type, values(any()))'
4142
]

library/core/src/ffi/primitives.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ mod c_char_definition {
105105
// architecture defaults). As we only have a target for userspace apps so there are no
106106
// special cases for L4Re below.
107107
// https://github.com/rust-lang/rust/pull/132975#issuecomment-2484645240
108-
if #[cfg(all(
108+
if #[cfg(any(c_char_type = "unsigned", all(
109+
not(c_char_type = "signed"),
109110
not(windows),
110111
not(target_vendor = "apple"),
111112
not(target_os = "vita"),
@@ -122,7 +123,7 @@ mod c_char_definition {
122123
target_arch = "s390x",
123124
target_arch = "xtensa",
124125
)
125-
))] {
126+
)))] {
126127
pub(super) type c_char = u8;
127128
} else {
128129
// On every other target, c_char is signed.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: unexpected `--cfg c_char_type="unsigned"` flag
2+
|
3+
= note: config `c_char_type` is only supposed to be controlled by `-Z c-char-type`
4+
= note: manually setting a built-in cfg can and does create incoherent behaviors
5+
= note: `#[deny(explicit_builtin_cfgs_in_flags)]` on by default
6+
7+
error: aborting due to 1 previous error
8+

tests/ui/cfg/disallowed-cli-cfgs.rs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
//@ revisions: target_thread_local_ relocation_model_
99
//@ revisions: fmt_debug_
1010
//@ revisions: emscripten_wasm_eh_
11+
//@ revisions: c_char_type_
1112

1213
//@ [overflow_checks_]compile-flags: --cfg overflow_checks
1314
//@ [debug_assertions_]compile-flags: --cfg debug_assertions
@@ -35,5 +36,6 @@
3536
//@ [relocation_model_]compile-flags: --cfg relocation_model="a"
3637
//@ [fmt_debug_]compile-flags: --cfg fmt_debug="shallow"
3738
//@ [emscripten_wasm_eh_]compile-flags: --cfg emscripten_wasm_eh
39+
//@ [c_char_type_]compile-flags: --cfg c_char_type="unsigned"
3840

3941
fn main() {}

tests/ui/check-cfg/cargo-build-script.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ warning: unexpected `cfg` condition name: `has_foo`
44
LL | #[cfg(has_foo)]
55
| ^^^^^^^
66
|
7-
= help: expected names are: `has_bar` and 31 more
7+
= help: expected names are: `has_bar` and 32 more
88
= help: consider using a Cargo feature instead
99
= help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:
1010
[lints.rust]

tests/ui/check-cfg/cargo-feature.none.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ warning: unexpected `cfg` condition name: `tokio_unstable`
2525
LL | #[cfg(tokio_unstable)]
2626
| ^^^^^^^^^^^^^^
2727
|
28-
= help: expected names are: `docsrs`, `feature`, and `test` and 31 more
28+
= help: expected names are: `docsrs`, `feature`, and `test` and 32 more
2929
= help: consider using a Cargo feature instead
3030
= help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:
3131
[lints.rust]

tests/ui/check-cfg/cargo-feature.some.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ warning: unexpected `cfg` condition name: `tokio_unstable`
2525
LL | #[cfg(tokio_unstable)]
2626
| ^^^^^^^^^^^^^^
2727
|
28-
= help: expected names are: `CONFIG_NVME`, `docsrs`, `feature`, and `test` and 31 more
28+
= help: expected names are: `CONFIG_NVME`, `docsrs`, `feature`, and `test` and 32 more
2929
= help: consider using a Cargo feature instead
3030
= help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:
3131
[lints.rust]

tests/ui/check-cfg/cfg-value-for-cfg-name-duplicate.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ warning: unexpected `cfg` condition name: `value`
44
LL | #[cfg(value)]
55
| ^^^^^
66
|
7-
= help: expected names are: `bar`, `bee`, `cow`, and `foo` and 31 more
7+
= help: expected names are: `bar`, `bee`, `cow`, and `foo` and 32 more
88
= help: to expect this configuration use `--check-cfg=cfg(value)`
99
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
1010
= note: `#[warn(unexpected_cfgs)]` on by default

tests/ui/check-cfg/cfg-value-for-cfg-name-multiple.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ warning: unexpected `cfg` condition name: `my_value`
44
LL | #[cfg(my_value)]
55
| ^^^^^^^^
66
|
7-
= help: expected names are: `bar` and `foo` and 31 more
7+
= help: expected names are: `bar` and `foo` and 32 more
88
= help: to expect this configuration use `--check-cfg=cfg(my_value)`
99
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
1010
= note: `#[warn(unexpected_cfgs)]` on by default

tests/ui/check-cfg/exhaustive-names-values.feature.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ warning: unexpected `cfg` condition name: `unknown_key`
44
LL | #[cfg(unknown_key = "value")]
55
| ^^^^^^^^^^^^^^^^^^^^^
66
|
7-
= help: expected names are: `feature` and 31 more
7+
= help: expected names are: `feature` and 32 more
88
= help: to expect this configuration use `--check-cfg=cfg(unknown_key, values("value"))`
99
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
1010
= note: `#[warn(unexpected_cfgs)]` on by default

tests/ui/check-cfg/exhaustive-names-values.full.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ warning: unexpected `cfg` condition name: `unknown_key`
44
LL | #[cfg(unknown_key = "value")]
55
| ^^^^^^^^^^^^^^^^^^^^^
66
|
7-
= help: expected names are: `feature` and 31 more
7+
= help: expected names are: `feature` and 32 more
88
= help: to expect this configuration use `--check-cfg=cfg(unknown_key, values("value"))`
99
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
1010
= note: `#[warn(unexpected_cfgs)]` on by default

tests/ui/check-cfg/mix.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ warning: unexpected `cfg` condition name: `uu`
4444
LL | #[cfg_attr(uu, unix)]
4545
| ^^
4646
|
47-
= help: expected names are: `feature` and 31 more
47+
= help: expected names are: `feature` and 32 more
4848
= help: to expect this configuration use `--check-cfg=cfg(uu)`
4949
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
5050

tests/ui/check-cfg/raw-keywords.edition2015.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ warning: unexpected `cfg` condition name: `r#false`
1414
LL | #[cfg(r#false)]
1515
| ^^^^^^^
1616
|
17-
= help: expected names are: `async`, `edition2015`, `edition2021`, and `r#true` and 31 more
17+
= help: expected names are: `async`, `edition2015`, `edition2021`, and `r#true` and 32 more
1818
= help: to expect this configuration use `--check-cfg=cfg(r#false)`
1919
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
2020

tests/ui/check-cfg/raw-keywords.edition2021.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ warning: unexpected `cfg` condition name: `r#false`
1414
LL | #[cfg(r#false)]
1515
| ^^^^^^^
1616
|
17-
= help: expected names are: `r#async`, `edition2015`, `edition2021`, and `r#true` and 31 more
17+
= help: expected names are: `r#async`, `edition2015`, `edition2021`, and `r#true` and 32 more
1818
= help: to expect this configuration use `--check-cfg=cfg(r#false)`
1919
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
2020

tests/ui/check-cfg/report-in-external-macros.cargo.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ warning: unexpected `cfg` condition name: `my_lib_cfg`
44
LL | cfg_macro::my_lib_macro!();
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
7-
= help: expected names are: `feature` and 31 more
7+
= help: expected names are: `feature` and 32 more
88
= note: using a cfg inside a macro will use the cfgs from the destination crate and not the ones from the defining crate
99
= help: try referring to `cfg_macro::my_lib_macro` crate for guidance on how handle this unexpected cfg
1010
= help: the macro `cfg_macro::my_lib_macro` may come from an old version of the `cfg_macro` crate, try updating your dependency with `cargo update -p cfg_macro`

tests/ui/check-cfg/report-in-external-macros.rustc.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ warning: unexpected `cfg` condition name: `my_lib_cfg`
44
LL | cfg_macro::my_lib_macro!();
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
7-
= help: expected names are: `feature` and 31 more
7+
= help: expected names are: `feature` and 32 more
88
= note: using a cfg inside a macro will use the cfgs from the destination crate and not the ones from the defining crate
99
= help: try referring to `cfg_macro::my_lib_macro` crate for guidance on how handle this unexpected cfg
1010
= help: to expect this configuration use `--check-cfg=cfg(my_lib_cfg)`

tests/ui/check-cfg/well-known-names.stderr

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ warning: unexpected `cfg` condition name: `list_all_well_known_cfgs`
44
LL | #[cfg(list_all_well_known_cfgs)]
55
| ^^^^^^^^^^^^^^^^^^^^^^^^
66
|
7-
= help: expected names are: `clippy`
7+
= help: expected names are: `c_char_type`
8+
`clippy`
89
`contract_checks`
910
`debug_assertions`
1011
`doc`

tests/ui/check-cfg/well-known-values.rs

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
// diagnostic prints the list of expected values.
2727
#[cfg(any(
2828
// tidy-alphabetical-start
29+
c_char_type = "_UNEXPECTED_VALUE",
30+
//~^ WARN unexpected `cfg` condition value
2931
clippy = "_UNEXPECTED_VALUE",
3032
//~^ WARN unexpected `cfg` condition value
3133
debug_assertions = "_UNEXPECTED_VALUE",

0 commit comments

Comments
 (0)