Skip to content

Commit 78285ef

Browse files
committed
XXX: add ByteSymbol
1 parent 111e9bc commit 78285ef

File tree

42 files changed

+372
-139
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+372
-139
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use rustc_data_structures::tagged_ptr::Tag;
3232
use rustc_macros::{Decodable, Encodable, HashStable_Generic};
3333
pub use rustc_span::AttrId;
3434
use rustc_span::source_map::{Spanned, respan};
35-
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Ident, Span, Symbol, kw, sym};
35+
use rustc_span::{ByteSymbol, DUMMY_SP, ErrorGuaranteed, Ident, Span, Symbol, kw, sym};
3636
use thin_vec::{ThinVec, thin_vec};
3737

3838
pub use crate::format::*;
@@ -1804,7 +1804,7 @@ pub enum ExprKind {
18041804
/// Added for optimization purposes to avoid the need to escape
18051805
/// large binary blobs - should always behave like [`ExprKind::Lit`]
18061806
/// with a `ByteStr` literal.
1807-
IncludedBytes(Arc<[u8]>),
1807+
IncludedBytes(Arc<[u8]>), // njn: change to ByteSymbol?
18081808

18091809
/// A `format_args!()` expression.
18101810
FormatArgs(P<FormatArgs>),
@@ -2062,7 +2062,8 @@ impl YieldKind {
20622062
}
20632063

20642064
/// A literal in a meta item.
2065-
#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
2065+
// njn: look for clones
2066+
#[derive(Clone, Copy, Encodable, Decodable, Debug, HashStable_Generic)]
20662067
pub struct MetaItemLit {
20672068
/// The original literal as written in the source code.
20682069
pub symbol: Symbol,
@@ -2125,16 +2126,17 @@ pub enum LitFloatType {
21252126
/// deciding the `LitKind`. This means that float literals like `1f32` are
21262127
/// classified by this type as `Float`. This is different to `token::LitKind`
21272128
/// which does *not* consider the suffix.
2128-
#[derive(Clone, Encodable, Decodable, Debug, Hash, Eq, PartialEq, HashStable_Generic)]
2129+
#[derive(Clone, Copy, Encodable, Decodable, Debug, Hash, Eq, PartialEq, HashStable_Generic)]
2130+
// njn: look for clones
21292131
pub enum LitKind {
21302132
/// A string literal (`"foo"`). The symbol is unescaped, and so may differ
21312133
/// from the original token's symbol.
21322134
Str(Symbol, StrStyle),
21332135
/// A byte string (`b"foo"`). Not stored as a symbol because it might be
21342136
/// non-utf8, and symbols only allow utf8 strings.
2135-
ByteStr(Arc<[u8]>, StrStyle),
2137+
ByteStr(ByteSymbol, StrStyle),
21362138
/// A C String (`c"foo"`). Guaranteed to only have `\0` at the end.
2137-
CStr(Arc<[u8]>, StrStyle),
2139+
CStr(ByteSymbol, StrStyle),
21382140
/// A byte char (`b'f'`).
21392141
Byte(u8),
21402142
/// A character literal (`'a'`).

compiler/rustc_ast/src/util/literal.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{ascii, fmt, str};
55
use rustc_literal_escaper::{
66
MixedUnit, Mode, byte_from_char, unescape_byte, unescape_char, unescape_mixed, unescape_unicode,
77
};
8-
use rustc_span::{Span, Symbol, kw, sym};
8+
use rustc_span::{ByteSymbol, Span, Symbol, kw, sym};
99
use tracing::debug;
1010

1111
use crate::ast::{self, LitKind, MetaItemLit, StrStyle};
@@ -117,13 +117,13 @@ impl LitKind {
117117
assert!(!err.is_fatal(), "failed to unescape string literal")
118118
}
119119
});
120-
LitKind::ByteStr(buf.into(), StrStyle::Cooked)
120+
LitKind::ByteStr(ByteSymbol::intern(&buf), StrStyle::Cooked)
121121
}
122122
token::ByteStrRaw(n) => {
123123
// Raw strings have no escapes so we can convert the symbol
124124
// directly to a `Arc<u8>`.
125125
let buf = symbol.as_str().to_owned().into_bytes();
126-
LitKind::ByteStr(buf.into(), StrStyle::Raw(n))
126+
LitKind::ByteStr(ByteSymbol::intern(&buf), StrStyle::Raw(n))
127127
}
128128
token::CStr => {
129129
let s = symbol.as_str();
@@ -138,15 +138,15 @@ impl LitKind {
138138
}
139139
});
140140
buf.push(0);
141-
LitKind::CStr(buf.into(), StrStyle::Cooked)
141+
LitKind::CStr(ByteSymbol::intern(&buf), StrStyle::Cooked)
142142
}
143143
token::CStrRaw(n) => {
144144
// Raw strings have no escapes so we can convert the symbol
145145
// directly to a `Arc<u8>` after appending the terminating NUL
146146
// char.
147147
let mut buf = symbol.as_str().to_owned().into_bytes();
148148
buf.push(0);
149-
LitKind::CStr(buf.into(), StrStyle::Raw(n))
149+
LitKind::CStr(ByteSymbol::intern(&buf), StrStyle::Raw(n))
150150
}
151151
token::Err(guar) => LitKind::Err(guar),
152152
})
@@ -169,11 +169,11 @@ impl fmt::Display for LitKind {
169169
string = sym
170170
)?,
171171
LitKind::ByteStr(ref bytes, StrStyle::Cooked) => {
172-
write!(f, "b\"{}\"", escape_byte_str_symbol(bytes))?
172+
write!(f, "b\"{}\"", escape_byte_str_symbol(bytes.as_byte_str()))?
173173
}
174174
LitKind::ByteStr(ref bytes, StrStyle::Raw(n)) => {
175175
// Unwrap because raw byte string literals can only contain ASCII.
176-
let symbol = str::from_utf8(bytes).unwrap();
176+
let symbol = str::from_utf8(bytes.as_byte_str()).unwrap();
177177
write!(
178178
f,
179179
"br{delim}\"{string}\"{delim}",
@@ -182,11 +182,11 @@ impl fmt::Display for LitKind {
182182
)?;
183183
}
184184
LitKind::CStr(ref bytes, StrStyle::Cooked) => {
185-
write!(f, "c\"{}\"", escape_byte_str_symbol(bytes))?
185+
write!(f, "c\"{}\"", escape_byte_str_symbol(bytes.as_byte_str()))?
186186
}
187187
LitKind::CStr(ref bytes, StrStyle::Raw(n)) => {
188188
// This can only be valid UTF-8.
189-
let symbol = str::from_utf8(bytes).unwrap();
189+
let symbol = str::from_utf8(bytes.as_byte_str()).unwrap();
190190
write!(f, "cr{delim}\"{symbol}\"{delim}", delim = "#".repeat(n as usize),)?;
191191
}
192192
LitKind::Int(n, ty) => {

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_middle::span_bug;
1212
use rustc_middle::ty::TyCtxt;
1313
use rustc_session::errors::report_lit_error;
1414
use rustc_span::source_map::{Spanned, respan};
15-
use rustc_span::{DUMMY_SP, DesugaringKind, Ident, Span, Symbol, sym};
15+
use rustc_span::{ByteSymbol, DUMMY_SP, DesugaringKind, Ident, Span, Symbol, sym};
1616
use thin_vec::{ThinVec, thin_vec};
1717
use visit::{Visitor, walk_expr};
1818

@@ -144,10 +144,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
144144
}
145145
ExprKind::Lit(token_lit) => hir::ExprKind::Lit(self.lower_lit(token_lit, e.span)),
146146
ExprKind::IncludedBytes(bytes) => {
147-
let lit = self.arena.alloc(respan(
147+
let lit = respan(
148148
self.lower_span(e.span),
149-
LitKind::ByteStr(Arc::clone(bytes), StrStyle::Cooked),
150-
));
149+
LitKind::ByteStr(ByteSymbol::intern(&bytes), StrStyle::Cooked),
150+
);
151151
hir::ExprKind::Lit(lit)
152152
}
153153
ExprKind::Cast(expr, ty) => {
@@ -420,19 +420,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
420420
})
421421
}
422422

423-
pub(crate) fn lower_lit(
424-
&mut self,
425-
token_lit: &token::Lit,
426-
span: Span,
427-
) -> &'hir Spanned<LitKind> {
423+
pub(crate) fn lower_lit(&mut self, token_lit: &token::Lit, span: Span) -> hir::Lit {
428424
let lit_kind = match LitKind::from_token_lit(*token_lit) {
429425
Ok(lit_kind) => lit_kind,
430426
Err(err) => {
431427
let guar = report_lit_error(&self.tcx.sess.psess, err, *token_lit, span);
432428
LitKind::Err(guar)
433429
}
434430
};
435-
self.arena.alloc(respan(self.lower_span(span), lit_kind))
431+
respan(self.lower_span(span), lit_kind)
436432
}
437433

438434
fn lower_unop(&mut self, u: UnOp) -> hir::UnOp {
@@ -2140,10 +2136,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
21402136
}
21412137

21422138
fn expr_uint(&mut self, sp: Span, ty: ast::UintTy, value: u128) -> hir::Expr<'hir> {
2143-
let lit = self.arena.alloc(hir::Lit {
2139+
let lit = hir::Lit {
21442140
span: sp,
21452141
node: ast::LitKind::Int(value.into(), ast::LitIntType::Unsigned(ty)),
2146-
});
2142+
};
21472143
self.expr(sp, hir::ExprKind::Lit(lit))
21482144
}
21492145

@@ -2160,9 +2156,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
21602156
}
21612157

21622158
pub(super) fn expr_str(&mut self, sp: Span, value: Symbol) -> hir::Expr<'hir> {
2163-
let lit = self
2164-
.arena
2165-
.alloc(hir::Lit { span: sp, node: ast::LitKind::Str(value, ast::StrStyle::Cooked) });
2159+
let lit = hir::Lit { span: sp, node: ast::LitKind::Str(value, ast::StrStyle::Cooked) };
21662160
self.expr(sp, hir::ExprKind::Lit(lit))
21672161
}
21682162

compiler/rustc_ast_lowering/src/pat.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_hir::def::{DefKind, Res};
77
use rustc_hir::{self as hir, LangItem};
88
use rustc_middle::span_bug;
99
use rustc_span::source_map::{Spanned, respan};
10-
use rustc_span::{DesugaringKind, Ident, Span};
10+
use rustc_span::{ByteSymbol, DesugaringKind, Ident, Span};
1111

1212
use super::errors::{
1313
ArbitraryExpressionInPattern, ExtraDoubleDot, MisplacedDoubleDot, SubTupleBinding,
@@ -390,19 +390,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
390390
allow_paths: bool,
391391
) -> &'hir hir::PatExpr<'hir> {
392392
let span = self.lower_span(expr.span);
393-
let err = |guar| hir::PatExprKind::Lit {
394-
lit: self.arena.alloc(respan(span, LitKind::Err(guar))),
395-
negated: false,
396-
};
393+
let err =
394+
|guar| hir::PatExprKind::Lit { lit: respan(span, LitKind::Err(guar)), negated: false };
397395
let kind = match &expr.kind {
398396
ExprKind::Lit(lit) => {
399397
hir::PatExprKind::Lit { lit: self.lower_lit(lit, span), negated: false }
400398
}
401399
ExprKind::ConstBlock(c) => hir::PatExprKind::ConstBlock(self.lower_const_block(c)),
402400
ExprKind::IncludedBytes(bytes) => hir::PatExprKind::Lit {
403-
lit: self
404-
.arena
405-
.alloc(respan(span, LitKind::ByteStr(Arc::clone(bytes), StrStyle::Cooked))),
401+
lit: respan(span, LitKind::ByteStr(ByteSymbol::intern(bytes), StrStyle::Cooked)),
406402
negated: false,
407403
},
408404
ExprKind::Err(guar) => err(*guar),

compiler/rustc_builtin_macros/src/concat_bytes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ pub(crate) fn expand_concat_bytes(
178178
accumulator.push(val);
179179
}
180180
Ok(LitKind::ByteStr(ref bytes, _)) => {
181-
accumulator.extend_from_slice(bytes);
181+
accumulator.extend_from_slice(bytes.as_byte_str());
182182
}
183183
_ => {
184184
guar.get_or_insert_with(|| invalid_type_err(cx, token_lit, e.span, false));

compiler/rustc_hir/src/arena.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ macro_rules! arena_types {
88
[] asm_template: rustc_ast::InlineAsmTemplatePiece,
99
[] attribute: rustc_hir::Attribute,
1010
[] owner_info: rustc_hir::OwnerInfo<'tcx>,
11-
[] lit: rustc_hir::Lit,
1211
[] macro_def: rustc_ast::MacroDef,
1312
]);
1413
)

compiler/rustc_hir/src/hir.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1807,7 +1807,7 @@ pub struct PatExpr<'hir> {
18071807
#[derive(Debug, Clone, Copy, HashStable_Generic)]
18081808
pub enum PatExprKind<'hir> {
18091809
Lit {
1810-
lit: &'hir Lit,
1810+
lit: Lit,
18111811
// FIXME: move this into `Lit` and handle negated literal expressions
18121812
// once instead of matching on unop neg expressions everywhere.
18131813
negated: bool,
@@ -2734,7 +2734,7 @@ pub enum ExprKind<'hir> {
27342734
/// A unary operation (e.g., `!x`, `*x`).
27352735
Unary(UnOp, &'hir Expr<'hir>),
27362736
/// A literal (e.g., `1`, `"foo"`).
2737-
Lit(&'hir Lit),
2737+
Lit(Lit),
27382738
/// A cast (e.g., `foo as f64`).
27392739
Cast(&'hir Expr<'hir>, &'hir Ty<'hir>),
27402740
/// A type ascription (e.g., `x: Foo`). See RFC 3307.

compiler/rustc_hir/src/intravisit.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ pub trait Visitor<'v>: Sized {
347347
fn visit_pat_expr(&mut self, expr: &'v PatExpr<'v>) -> Self::Result {
348348
walk_pat_expr(self, expr)
349349
}
350-
fn visit_lit(&mut self, _hir_id: HirId, _lit: &'v Lit, _negated: bool) -> Self::Result {
350+
fn visit_lit(&mut self, _hir_id: HirId, _lit: Lit, _negated: bool) -> Self::Result {
351351
Self::Result::output()
352352
}
353353
fn visit_anon_const(&mut self, c: &'v AnonConst) -> Self::Result {
@@ -786,7 +786,7 @@ pub fn walk_pat_expr<'v, V: Visitor<'v>>(visitor: &mut V, expr: &'v PatExpr<'v>)
786786
let PatExpr { hir_id, span, kind } = expr;
787787
try_visit!(visitor.visit_id(*hir_id));
788788
match kind {
789-
PatExprKind::Lit { lit, negated } => visitor.visit_lit(*hir_id, lit, *negated),
789+
PatExprKind::Lit { lit, negated } => visitor.visit_lit(*hir_id, *lit, *negated),
790790
PatExprKind::ConstBlock(c) => visitor.visit_inline_const(c),
791791
PatExprKind::Path(qpath) => visitor.visit_qpath(qpath, *hir_id, *span),
792792
}

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2364,9 +2364,9 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
23642364
};
23652365

23662366
let lit_input = match expr.kind {
2367-
hir::ExprKind::Lit(lit) => Some(LitToConstInput { lit: &lit.node, ty, neg: false }),
2367+
hir::ExprKind::Lit(lit) => Some(LitToConstInput { lit: lit.node, ty, neg: false }),
23682368
hir::ExprKind::Unary(hir::UnOp::Neg, expr) => match expr.kind {
2369-
hir::ExprKind::Lit(lit) => Some(LitToConstInput { lit: &lit.node, ty, neg: true }),
2369+
hir::ExprKind::Lit(lit) => Some(LitToConstInput { lit: lit.node, ty, neg: true }),
23702370
_ => None,
23712371
},
23722372
_ => None,

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1476,7 +1476,7 @@ impl<'a> State<'a> {
14761476
self.print_expr_addr_of(k, m, expr);
14771477
}
14781478
hir::ExprKind::Lit(lit) => {
1479-
self.print_literal(lit);
1479+
self.print_literal(&lit);
14801480
}
14811481
hir::ExprKind::Cast(expr, ty) => {
14821482
self.print_expr_cond_paren(expr, self.precedence(expr) < ExprPrecedence::Cast);

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1631,10 +1631,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
16311631

16321632
match lit.node {
16331633
ast::LitKind::Str(..) => Ty::new_static_str(tcx),
1634+
// njn: why is this an array, not a slice?
16341635
ast::LitKind::ByteStr(ref v, _) => Ty::new_imm_ref(
16351636
tcx,
16361637
tcx.lifetimes.re_static,
1637-
Ty::new_array(tcx, tcx.types.u8, v.len() as u64),
1638+
Ty::new_array(tcx, tcx.types.u8, v.as_byte_str().len() as u64),
16381639
),
16391640
ast::LitKind::Byte(_) => tcx.types.u8,
16401641
ast::LitKind::Char(_) => tcx.types.char,

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,7 +1624,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
16241624
node: rustc_ast::LitKind::Int(lit, rustc_ast::LitIntType::Unsuffixed),
16251625
span,
16261626
}) => {
1627-
let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(*span) else {
1627+
let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) else {
16281628
return false;
16291629
};
16301630
if !(snippet.starts_with("0x") || snippet.starts_with("0X")) {
@@ -1683,7 +1683,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
16831683

16841684
// We have satisfied all requirements to provide a suggestion. Emit it.
16851685
err.span_suggestion(
1686-
*span,
1686+
span,
16871687
format!("if you meant to create a null pointer, use `{null_path_str}()`"),
16881688
null_path_str + "()",
16891689
Applicability::MachineApplicable,

compiler/rustc_lint/src/invalid_from_utf8.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,9 @@ impl<'tcx> LateLintPass<'tcx> for InvalidFromUtf8 {
108108
}
109109
match init.kind {
110110
ExprKind::Lit(Spanned { node: lit, .. }) => {
111+
// njn: rename bytes as byte_sym, here and elsewhere
111112
if let LitKind::ByteStr(bytes, _) = &lit
112-
&& let Err(utf8_error) = std::str::from_utf8(bytes)
113+
&& let Err(utf8_error) = std::str::from_utf8(bytes.as_byte_str())
113114
{
114115
lint(init.span, utf8_error);
115116
}

compiler/rustc_lint/src/late.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ impl<'tcx, T: LateLintPass<'tcx>> hir_visit::Visitor<'tcx> for LateContextAndPas
152152
hir_visit::walk_pat(self, p);
153153
}
154154

155-
fn visit_lit(&mut self, hir_id: HirId, lit: &'tcx hir::Lit, negated: bool) {
155+
fn visit_lit(&mut self, hir_id: HirId, lit: hir::Lit, negated: bool) {
156156
lint_callback!(self, check_lit, hir_id, lit, negated);
157157
}
158158

compiler/rustc_lint/src/passes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ macro_rules! late_lint_methods {
2323
fn check_stmt(a: &'tcx rustc_hir::Stmt<'tcx>);
2424
fn check_arm(a: &'tcx rustc_hir::Arm<'tcx>);
2525
fn check_pat(a: &'tcx rustc_hir::Pat<'tcx>);
26-
fn check_lit(hir_id: rustc_hir::HirId, a: &'tcx rustc_hir::Lit, negated: bool);
26+
fn check_lit(hir_id: rustc_hir::HirId, a: rustc_hir::Lit, negated: bool);
2727
fn check_expr(a: &'tcx rustc_hir::Expr<'tcx>);
2828
fn check_expr_post(a: &'tcx rustc_hir::Expr<'tcx>);
2929
fn check_ty(a: &'tcx rustc_hir::Ty<'tcx, rustc_hir::AmbigArg>);

compiler/rustc_lint/src/types.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -547,18 +547,12 @@ fn lint_fn_pointer<'tcx>(
547547
}
548548

549549
impl<'tcx> LateLintPass<'tcx> for TypeLimits {
550-
fn check_lit(
551-
&mut self,
552-
cx: &LateContext<'tcx>,
553-
hir_id: HirId,
554-
lit: &'tcx hir::Lit,
555-
negated: bool,
556-
) {
550+
fn check_lit(&mut self, cx: &LateContext<'tcx>, hir_id: HirId, lit: hir::Lit, negated: bool) {
557551
if negated {
558552
self.negated_expr_id = Some(hir_id);
559553
self.negated_expr_span = Some(lit.span);
560554
}
561-
lint_literal(cx, self, hir_id, lit.span, lit, negated);
555+
lint_literal(cx, self, hir_id, lit.span, &lit, negated);
562556
}
563557

564558
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx hir::Expr<'tcx>) {

0 commit comments

Comments
 (0)