Skip to content

Commit 76d1f93

Browse files
committed
update and add a few tests
1 parent 8ff3903 commit 76d1f93

File tree

11 files changed

+48
-3
lines changed

11 files changed

+48
-3
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1814,7 +1814,7 @@ pub enum LitKind {
18141814
/// A byte string (`b"foo"`). Not stored as a symbol because it might be
18151815
/// non-utf8, and symbols only allow utf8 strings.
18161816
ByteStr(Lrc<[u8]>, StrStyle),
1817-
/// A C String (`c"foo"`).
1817+
/// A C String (`c"foo"`). Guaranteed only have `\0` in the end.
18181818
CStr(Lrc<[u8]>, StrStyle),
18191819
/// A byte char (`b'f'`).
18201820
Byte(u8),

compiler/rustc_ast/src/util/literal.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ impl LitKind {
181181
}
182182
});
183183
error?;
184-
buf.push(b'\0');
184+
buf.push(0);
185185
LitKind::CStr(buf.into(), StrStyle::Cooked)
186186
}
187187
token::CStrRaw(n) => {
@@ -204,7 +204,7 @@ impl LitKind {
204204
}
205205
});
206206
error?;
207-
buf.push(b'\0');
207+
buf.push(0);
208208
LitKind::CStr(buf.into(), StrStyle::Raw(n))
209209
}
210210
token::Err => LitKind::Err,

compiler/rustc_ast_passes/src/feature_gate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use ast::token;
12
use rustc_ast as ast;
23
use rustc_ast::visit::{self, AssocCtxt, FnCtxt, FnKind, Visitor};
34
use rustc_ast::{attr, AssocConstraint, AssocConstraintKind, NodeId};

compiler/rustc_feature/src/active.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,8 @@ declare_features! (
313313
(active, async_fn_in_trait, "1.66.0", Some(91611), None),
314314
/// Treat `extern "C"` function as nounwind.
315315
(active, c_unwind, "1.52.0", Some(74990), None),
316+
/// Allows `c"foo"` literals.
317+
(active, c_str_literals, "CURRENT_RUSTC_VERSION", Some(105723), None),
316318
/// Allows using C-variadics.
317319
(active, c_variadic, "1.34.0", Some(44930), None),
318320
/// Allows the use of `#[cfg(sanitize = "option")]`; set when -Zsanitizer is used.

compiler/rustc_mir_build/src/build/expr/as_constant.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@ pub(crate) fn lit_to_mir_constant<'tcx>(
146146
let id = tcx.allocate_bytes(data);
147147
ConstValue::Scalar(Scalar::from_pointer(id.into(), &tcx))
148148
}
149+
(ast::LitKind::CStr(data, _), ty::Ref(_, inner_ty, _)) if matches!(inner_ty.kind(), ty::Adt(def, _) if Some(def.did()) == tcx.lang_items().c_str()) =>
150+
{
151+
let allocation = Allocation::from_bytes_byte_aligned_immutable(data as &[u8]);
152+
let allocation = tcx.mk_const_alloc(allocation);
153+
ConstValue::Slice { data: allocation, start: 0, end: data.len() }
154+
}
149155
(ast::LitKind::Byte(n), ty::Uint(ty::UintTy::U8)) => {
150156
ConstValue::Scalar(Scalar::from_uint(*n, Size::from_bytes(1)))
151157
}

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,7 @@ symbols! {
441441
bridge,
442442
bswap,
443443
c_str,
444+
c_str_literals,
444445
c_unwind,
445446
c_variadic,
446447
call,
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// run-pass
2+
3+
#![feature(c_str_literals)]
4+
5+
fn main() {
6+
assert_eq!(b"test\0", c"test".to_bytes_with_nul());
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn main() {
2+
c"foo";
3+
//~^ ERROR: `c".."` literals are experimental
4+
5+
m!(c"test");
6+
//~^ ERROR: `c".."` literals are experimental
7+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0658]: `c".."` literals are experimental
2+
--> $DIR/gate.rs:8:5
3+
|
4+
LL | c"foo";
5+
| ^^^^^^
6+
|
7+
= note: see issue #105723 <https://github.com/rust-lang/rust/issues/105723> for more information
8+
= help: add `#![feature(c_str_literals)]` to the crate attributes to enable
9+
10+
error[E0658]: `c".."` literals are experimental
11+
--> $DIR/gate.rs:11:8
12+
|
13+
LL | m!(c"test");
14+
| ^^^^^^^
15+
|
16+
= note: see issue #105723 <https://github.com/rust-lang/rust/issues/105723> for more information
17+
= help: add `#![feature(c_str_literals)]` to the crate attributes to enable
18+
19+
error: aborting due to 2 previous errors
20+
21+
For more information about this error, try `rustc --explain E0658`.
322 Bytes
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)