Skip to content

Commit 876501b

Browse files
committed
-Zc-char-type=unsigned|signed|default flag for c_char->u8/i8 selection override
1 parent 2b4694a commit 876501b

29 files changed

+255
-74
lines changed

Diff for: compiler/rustc_feature/src/builtin_attrs.rs

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const GATED_CFGS: &[GatedCfg] = &[
3939
// this is consistent with naming of the compiler flag it's for
4040
(sym::fmt_debug, sym::fmt_debug, Features::fmt_debug),
4141
(sym::emscripten_wasm_eh, sym::cfg_emscripten_wasm_eh, Features::cfg_emscripten_wasm_eh),
42+
(sym::c_char_type, sym::c_char_type, Features::c_char_type),
4243
];
4344

4445
/// Find a gated cfg determined by the `pred`icate which is given the cfg's name.

Diff for: compiler/rustc_feature/src/unstable.rs

+2
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,8 @@ declare_features! (
390390
(unstable, async_for_loop, "1.77.0", Some(118898)),
391391
/// Allows `async` trait bound modifier.
392392
(unstable, async_trait_bounds, "1.85.0", Some(62290)),
393+
// Allows customized c_char type (u8/i8)
394+
(unstable, c_char_type, "1.86.0", Some(138446)),
393395
/// Allows using C-variadics.
394396
(unstable, c_variadic, "1.34.0", Some(44930)),
395397
/// Allows the use of `#[cfg(<true/false>)]`.

Diff for: compiler/rustc_interface/src/tests.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_data_structures::profiling::TimePassesFormat;
99
use rustc_errors::emitter::HumanReadableErrorType;
1010
use rustc_errors::{ColorConfig, registry};
1111
use rustc_session::config::{
12-
AutoDiff, BranchProtection, CFGuard, Cfg, CollapseMacroDebuginfo, CoverageLevel,
12+
AutoDiff, BranchProtection, CCharType, CFGuard, Cfg, CollapseMacroDebuginfo, CoverageLevel,
1313
CoverageOptions, DebugInfo, DumpMonoStatsFormat, ErrorOutputType, ExternEntry, ExternLocation,
1414
Externs, FmtDebug, FunctionReturn, InliningThreshold, Input, InstrumentCoverage,
1515
InstrumentXRay, LinkSelfContained, LinkerPluginLto, LocationDetail, LtoCli, MirIncludeSpans,
@@ -769,6 +769,7 @@ fn test_unstable_options_tracking_hash() {
769769
pac_ret: Some(PacRet { leaf: true, pc: true, key: PAuthKey::B })
770770
})
771771
);
772+
tracked!(c_char_type, CCharType::Unsigned);
772773
tracked!(codegen_backend, Some("abc".to_string()));
773774
tracked!(
774775
coverage_options,

Diff for: 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+
}

Diff for: compiler/rustc_session/src/config/cfg.rs

+8-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,10 @@ pub(crate) fn default_configuration(sess: &Session) -> Cfg {
306307
ins_none!(sym::contract_checks);
307308
}
308309

310+
if sess.is_nightly_build() {
311+
ins_sym!(sym::c_char_type, sess.opts.unstable_opts.c_char_type.desc_symbol());
312+
}
313+
309314
ret
310315
}
311316

@@ -470,5 +475,7 @@ impl CheckCfg {
470475

471476
ins!(sym::unix, no_values);
472477
ins!(sym::windows, no_values);
478+
479+
ins!(sym::c_char_type, empty_values).extend(CCharType::all());
473480
}
474481
}

Diff for: 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],

Diff for: 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,

Diff for: library/core/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ optimize_for_size = []
2525
debug_refcell = []
2626
# Make `TypeId` store a reference to the name of the type, so that it can print that name.
2727
debug_typeid = []
28+
#Allow -Zc-char-type='unsigned|signed|default' for c_char override
29+
c_char_type = []
2830

2931
[lints.rust.unexpected_cfgs]
3032
level = "warn"
@@ -38,4 +40,5 @@ check-cfg = [
3840
# and to stdarch `core_arch` crate which messes-up with Cargo list
3941
# of declared features, we therefor expect any feature cfg
4042
'cfg(feature, values(any()))',
43+
'cfg(c_char_type, values(any()))'
4144
]

Diff for: library/core/src/ffi/primitives.rs

+52-21
Original file line numberDiff line numberDiff line change
@@ -105,28 +105,59 @@ 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(
109-
not(windows),
110-
not(target_vendor = "apple"),
111-
not(target_os = "vita"),
112-
any(
113-
target_arch = "aarch64",
114-
target_arch = "arm",
115-
target_arch = "csky",
116-
target_arch = "hexagon",
117-
target_arch = "msp430",
118-
target_arch = "powerpc",
119-
target_arch = "powerpc64",
120-
target_arch = "riscv32",
121-
target_arch = "riscv64",
122-
target_arch = "s390x",
123-
target_arch = "xtensa",
124-
)
125-
))] {
126-
pub(super) type c_char = u8;
108+
if #[cfg(feature = "c_char_type")] {
109+
cfg_if! {
110+
if #[cfg(any(c_char_type = "unsigned", all(
111+
not(c_char_type = "signed"),
112+
not(windows),
113+
not(target_vendor = "apple"),
114+
not(target_os = "vita"),
115+
any(
116+
target_arch = "aarch64",
117+
target_arch = "arm",
118+
target_arch = "csky",
119+
target_arch = "hexagon",
120+
target_arch = "msp430",
121+
target_arch = "powerpc",
122+
target_arch = "powerpc64",
123+
target_arch = "riscv32",
124+
target_arch = "riscv64",
125+
target_arch = "s390x",
126+
target_arch = "xtensa",
127+
)
128+
)))] {
129+
pub(super) type c_char = u8;
130+
} else {
131+
// On every other target, c_char is signed.
132+
pub(super) type c_char = i8;
133+
}
134+
}
127135
} else {
128-
// On every other target, c_char is signed.
129-
pub(super) type c_char = i8;
136+
cfg_if! {
137+
if #[cfg(all(
138+
not(windows),
139+
not(target_vendor = "apple"),
140+
not(target_os = "vita"),
141+
any(
142+
target_arch = "aarch64",
143+
target_arch = "arm",
144+
target_arch = "csky",
145+
target_arch = "hexagon",
146+
target_arch = "msp430",
147+
target_arch = "powerpc",
148+
target_arch = "powerpc64",
149+
target_arch = "riscv32",
150+
target_arch = "riscv64",
151+
target_arch = "s390x",
152+
target_arch = "xtensa",
153+
)
154+
))] {
155+
pub(super) type c_char = u8;
156+
} else {
157+
// On every other target, c_char is signed.
158+
pub(super) type c_char = i8;
159+
}
160+
}
130161
}
131162
}
132163
}

Diff for: tests/ui/cfg/disallowed-cli-cfgs.c_char_type_.stderr

+8
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+

Diff for: 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() {}

Diff for: 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]

Diff for: 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]

Diff for: 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]

Diff for: 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

Diff for: 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

Diff for: 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

Diff for: 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

Diff for: 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

Diff for: 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

0 commit comments

Comments
 (0)