Skip to content

Commit ba078e5

Browse files
committed
XXX: use ByteSymbol
1 parent 896275c commit ba078e5

File tree

42 files changed

+282
-143
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

+282
-143
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::*;
@@ -1766,7 +1766,7 @@ pub enum ExprKind {
17661766
/// Added for optimization purposes to avoid the need to escape
17671767
/// large binary blobs - should always behave like [`ExprKind::Lit`]
17681768
/// with a `ByteStr` literal.
1769-
IncludedBytes(Arc<[u8]>),
1769+
IncludedBytes(Arc<[u8]>), // njn: change to ByteSymbol?
17701770

17711771
/// A `format_args!()` expression.
17721772
FormatArgs(P<FormatArgs>),
@@ -2024,7 +2024,8 @@ impl YieldKind {
20242024
}
20252025

20262026
/// A literal in a meta item.
2027-
#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
2027+
// njn: look for clones
2028+
#[derive(Clone, Copy, Encodable, Decodable, Debug, HashStable_Generic)]
20282029
pub struct MetaItemLit {
20292030
/// The original literal as written in the source code.
20302031
pub symbol: Symbol,
@@ -2087,16 +2088,17 @@ pub enum LitFloatType {
20872088
/// deciding the `LitKind`. This means that float literals like `1f32` are
20882089
/// classified by this type as `Float`. This is different to `token::LitKind`
20892090
/// which does *not* consider the suffix.
2090-
#[derive(Clone, Encodable, Decodable, Debug, Hash, Eq, PartialEq, HashStable_Generic)]
2091+
#[derive(Clone, Copy, Encodable, Decodable, Debug, Hash, Eq, PartialEq, HashStable_Generic)]
2092+
// njn: look for clones
20912093
pub enum LitKind {
20922094
/// A string literal (`"foo"`). The symbol is unescaped, and so may differ
20932095
/// from the original token's symbol.
20942096
Str(Symbol, StrStyle),
20952097
/// A byte string (`b"foo"`). Not stored as a symbol because it might be
20962098
/// non-utf8, and symbols only allow utf8 strings.
2097-
ByteStr(Arc<[u8]>, StrStyle),
2099+
ByteStr(ByteSymbol, StrStyle),
20982100
/// A C String (`c"foo"`). Guaranteed to only have `\0` at the end.
2099-
CStr(Arc<[u8]>, StrStyle),
2101+
CStr(ByteSymbol, StrStyle),
21002102
/// A byte char (`b'f'`).
21012103
Byte(u8),
21022104
/// 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
@@ -13,7 +13,7 @@ use rustc_middle::span_bug;
1313
use rustc_middle::ty::TyCtxt;
1414
use rustc_session::errors::report_lit_error;
1515
use rustc_span::source_map::{Spanned, respan};
16-
use rustc_span::{DUMMY_SP, DesugaringKind, Ident, Span, Symbol, sym};
16+
use rustc_span::{ByteSymbol, DUMMY_SP, DesugaringKind, Ident, Span, Symbol, sym};
1717
use thin_vec::{ThinVec, thin_vec};
1818
use visit::{Visitor, walk_expr};
1919

@@ -146,10 +146,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
146146
}
147147
ExprKind::Lit(token_lit) => hir::ExprKind::Lit(self.lower_lit(token_lit, e.span)),
148148
ExprKind::IncludedBytes(bytes) => {
149-
let lit = self.arena.alloc(respan(
149+
let lit = respan(
150150
self.lower_span(e.span),
151-
LitKind::ByteStr(Arc::clone(bytes), StrStyle::Cooked),
152-
));
151+
LitKind::ByteStr(ByteSymbol::intern(&bytes), StrStyle::Cooked),
152+
);
153153
hir::ExprKind::Lit(lit)
154154
}
155155
ExprKind::Cast(expr, ty) => {
@@ -422,19 +422,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
422422
})
423423
}
424424

425-
pub(crate) fn lower_lit(
426-
&mut self,
427-
token_lit: &token::Lit,
428-
span: Span,
429-
) -> &'hir Spanned<LitKind> {
425+
pub(crate) fn lower_lit(&mut self, token_lit: &token::Lit, span: Span) -> hir::Lit {
430426
let lit_kind = match LitKind::from_token_lit(*token_lit) {
431427
Ok(lit_kind) => lit_kind,
432428
Err(err) => {
433429
let guar = report_lit_error(&self.tcx.sess.psess, err, *token_lit, span);
434430
LitKind::Err(guar)
435431
}
436432
};
437-
self.arena.alloc(respan(self.lower_span(span), lit_kind))
433+
respan(self.lower_span(span), lit_kind)
438434
}
439435

440436
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
@@ -158,7 +158,7 @@ pub(crate) fn expand_concat_bytes(
158158
accumulator.push(val);
159159
}
160160
Ok(LitKind::ByteStr(ref bytes, _)) => {
161-
accumulator.extend_from_slice(bytes);
161+
accumulator.extend_from_slice(bytes.as_byte_str());
162162
}
163163
_ => {
164164
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
@@ -9,7 +9,6 @@ macro_rules! arena_types {
99
[] attribute: rustc_hir::Attribute,
1010
[] owner_info: rustc_hir::OwnerInfo<'tcx>,
1111
[] use_path: rustc_hir::UsePath<'tcx>,
12-
[] lit: rustc_hir::Lit,
1312
[] macro_def: rustc_ast::MacroDef,
1413
]);
1514
)

compiler/rustc_hir/src/hir.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1809,7 +1809,7 @@ pub struct PatExpr<'hir> {
18091809
#[derive(Debug, Clone, Copy, HashStable_Generic)]
18101810
pub enum PatExprKind<'hir> {
18111811
Lit {
1812-
lit: &'hir Lit,
1812+
lit: Lit,
18131813
// FIXME: move this into `Lit` and handle negated literal expressions
18141814
// once instead of matching on unop neg expressions everywhere.
18151815
negated: bool,
@@ -2722,7 +2722,7 @@ pub enum ExprKind<'hir> {
27222722
/// A unary operation (e.g., `!x`, `*x`).
27232723
Unary(UnOp, &'hir Expr<'hir>),
27242724
/// A literal (e.g., `1`, `"foo"`).
2725-
Lit(&'hir Lit),
2725+
Lit(Lit),
27262726
/// A cast (e.g., `foo as f64`).
27272727
Cast(&'hir Expr<'hir>, &'hir Ty<'hir>),
27282728
/// 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
@@ -343,7 +343,7 @@ pub trait Visitor<'v>: Sized {
343343
fn visit_pat_expr(&mut self, expr: &'v PatExpr<'v>) -> Self::Result {
344344
walk_pat_expr(self, expr)
345345
}
346-
fn visit_lit(&mut self, _hir_id: HirId, _lit: &'v Lit, _negated: bool) -> Self::Result {
346+
fn visit_lit(&mut self, _hir_id: HirId, _lit: Lit, _negated: bool) -> Self::Result {
347347
Self::Result::output()
348348
}
349349
fn visit_anon_const(&mut self, c: &'v AnonConst) -> Self::Result {
@@ -768,7 +768,7 @@ pub fn walk_pat_field<'v, V: Visitor<'v>>(visitor: &mut V, field: &'v PatField<'
768768
pub fn walk_pat_expr<'v, V: Visitor<'v>>(visitor: &mut V, expr: &'v PatExpr<'v>) -> V::Result {
769769
try_visit!(visitor.visit_id(expr.hir_id));
770770
match &expr.kind {
771-
PatExprKind::Lit { lit, negated } => visitor.visit_lit(expr.hir_id, lit, *negated),
771+
PatExprKind::Lit { lit, negated } => visitor.visit_lit(expr.hir_id, *lit, *negated),
772772
PatExprKind::ConstBlock(c) => visitor.visit_inline_const(c),
773773
PatExprKind::Path(qpath) => visitor.visit_qpath(qpath, expr.hir_id, expr.span),
774774
}

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

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

24352435
let lit_input = match expr.kind {
2436-
hir::ExprKind::Lit(lit) => Some(LitToConstInput { lit: &lit.node, ty, neg: false }),
2436+
hir::ExprKind::Lit(lit) => Some(LitToConstInput { lit: lit.node, ty, neg: false }),
24372437
hir::ExprKind::Unary(hir::UnOp::Neg, expr) => match expr.kind {
2438-
hir::ExprKind::Lit(lit) => Some(LitToConstInput { lit: &lit.node, ty, neg: true }),
2438+
hir::ExprKind::Lit(lit) => Some(LitToConstInput { lit: lit.node, ty, neg: true }),
24392439
_ => None,
24402440
},
24412441
_ => None,

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1480,7 +1480,7 @@ impl<'a> State<'a> {
14801480
self.print_expr_addr_of(k, m, expr);
14811481
}
14821482
hir::ExprKind::Lit(lit) => {
1483-
self.print_literal(lit);
1483+
self.print_literal(&lit);
14841484
}
14851485
hir::ExprKind::Cast(expr, ty) => {
14861486
self.print_expr_cond_paren(expr, expr.precedence() < 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
@@ -548,18 +548,12 @@ fn lint_fn_pointer<'tcx>(
548548
}
549549

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

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

0 commit comments

Comments
 (0)