Skip to content

Commit 6709720

Browse files
committed
Introduce ByteSymbol.
It's like `Symbol` but for byte strings. The interner is now used for both `Symbol` and `ByteSymbol`. E.g. if you intern `"dog"` and `b"dog"` you'll get a `Symbol` and a `ByteSymbol` with the same index and the characters will only be stored once. The motivation for this is to eliminate the `Arc`s in `ast::LitKind`, to make `ast::LitKind` impl `Copy`, and to avoid the need to arena-allocate `ast::LitKind` in HIR. The latter change reduces peak memory by a non-trivial amount on literal-heavy benchmarks such as `deep-vector` and `tuple-stress`. `Encoder`, `Decoder`, `SpanEncoder`, and `SpanDecoder` all get some changes so that they can handle normal strings and byte strings. This change does slow down compilation of programs that use `include_bytes!` on large files, because the contents of those files are now interned (hashed). This makes `include_bytes!` more similar to `include_str!`, though `include_bytes!` contents still aren't escaped, and hashing is still much cheaper than escaping.
1 parent 06a87e9 commit 6709720

14 files changed

+28
-24
lines changed

clippy_lints/src/approx_const.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl ApproxConstant {
7474
}
7575

7676
impl LateLintPass<'_> for ApproxConstant {
77-
fn check_lit(&mut self, cx: &LateContext<'_>, _hir_id: HirId, lit: &Lit, _negated: bool) {
77+
fn check_lit(&mut self, cx: &LateContext<'_>, _hir_id: HirId, lit: Lit, _negated: bool) {
7878
match lit.node {
7979
LitKind::Float(s, LitFloatType::Suffixed(fty)) => match fty {
8080
FloatTy::F16 => self.check_known_consts(cx, lit.span, s, "f16"),

clippy_lints/src/bool_assert_comparison.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ fn extract_bool_lit(e: &Expr<'_>) -> Option<bool> {
4242
}) = e.kind
4343
&& !e.span.from_expansion()
4444
{
45-
Some(*b)
45+
Some(b)
4646
} else {
4747
None
4848
}

clippy_lints/src/casts/manual_dangling_ptr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, from: &Expr<'_>, to:
4646
fn is_expr_const_aligned(cx: &LateContext<'_>, expr: &Expr<'_>, to: &Ty<'_>) -> bool {
4747
match expr.kind {
4848
ExprKind::Call(fun, _) => is_align_of_call(cx, fun, to),
49-
ExprKind::Lit(lit) => is_literal_aligned(cx, lit, to),
49+
ExprKind::Lit(lit) => is_literal_aligned(cx, &lit, to),
5050
_ => false,
5151
}
5252
}

clippy_lints/src/casts/unnecessary_cast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ fn lint_unnecessary_cast(
243243
);
244244
}
245245

246-
fn get_numeric_literal<'e>(expr: &'e Expr<'e>) -> Option<&'e Lit> {
246+
fn get_numeric_literal<'e>(expr: &'e Expr<'e>) -> Option<Lit> {
247247
match expr.kind {
248248
ExprKind::Lit(lit) => Some(lit),
249249
ExprKind::Unary(UnOp::Neg, e) => {

clippy_lints/src/default_numeric_fallback.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl<'a, 'tcx> NumericFallbackVisitor<'a, 'tcx> {
8383
}
8484

8585
/// Check whether a passed literal has potential to cause fallback or not.
86-
fn check_lit(&self, lit: &Lit, lit_ty: Ty<'tcx>, emit_hir_id: HirId) {
86+
fn check_lit(&self, lit: Lit, lit_ty: Ty<'tcx>, emit_hir_id: HirId) {
8787
if !lit.span.in_external_macro(self.cx.sess().source_map())
8888
&& matches!(self.ty_bounds.last(), Some(ExplicitTyBound(false)))
8989
&& matches!(
@@ -210,7 +210,7 @@ impl<'tcx> Visitor<'tcx> for NumericFallbackVisitor<'_, 'tcx> {
210210

211211
ExprKind::Lit(lit) => {
212212
let ty = self.cx.typeck_results().expr_ty(expr);
213-
self.check_lit(lit, ty, expr.hir_id);
213+
self.check_lit(*lit, ty, expr.hir_id);
214214
return;
215215
},
216216

clippy_lints/src/large_include_file.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl LateLintPass<'_> for LargeIncludeFile {
5757
if let ExprKind::Lit(lit) = &expr.kind
5858
&& let len = match &lit.node {
5959
// include_bytes
60-
LitKind::ByteStr(bstr, _) => bstr.len(),
60+
LitKind::ByteStr(bstr, _) => bstr.as_byte_str().len(),
6161
// include_str
6262
LitKind::Str(sym, _) => sym.as_str().len(),
6363
_ => return,

clippy_lints/src/manual_ignore_case_cmp.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ declare_clippy_lint! {
4141

4242
declare_lint_pass!(ManualIgnoreCaseCmp => [MANUAL_IGNORE_CASE_CMP]);
4343

44-
enum MatchType<'a, 'b> {
44+
enum MatchType<'a> {
4545
ToAscii(bool, Ty<'a>),
46-
Literal(&'b LitKind),
46+
Literal(LitKind),
4747
}
4848

49-
fn get_ascii_type<'a, 'b>(cx: &LateContext<'a>, kind: rustc_hir::ExprKind<'b>) -> Option<(Span, MatchType<'a, 'b>)> {
49+
fn get_ascii_type<'a>(cx: &LateContext<'a>, kind: rustc_hir::ExprKind<'_>) -> Option<(Span, MatchType<'a>)> {
5050
if let MethodCall(path, expr, _, _) = kind {
5151
let is_lower = match path.ident.name {
5252
sym::to_ascii_lowercase => true,
@@ -63,7 +63,7 @@ fn get_ascii_type<'a, 'b>(cx: &LateContext<'a>, kind: rustc_hir::ExprKind<'b>) -
6363
return Some((expr.span, ToAscii(is_lower, ty_raw)));
6464
}
6565
} else if let Lit(expr) = kind {
66-
return Some((expr.span, Literal(&expr.node)));
66+
return Some((expr.span, Literal(expr.node)));
6767
}
6868
None
6969
}

clippy_lints/src/manual_strip.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ fn eq_pattern_length<'tcx>(cx: &LateContext<'tcx>, pattern: &Expr<'_>, expr: &'t
184184
..
185185
}) = expr.kind
186186
{
187-
constant_length(cx, pattern).is_some_and(|length| *n == length)
187+
constant_length(cx, pattern).is_some_and(|length| n == length)
188188
} else {
189189
len_arg(cx, expr).is_some_and(|arg| eq_expr_value(cx, pattern, arg))
190190
}

clippy_lints/src/matches/match_like_matches.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ fn find_bool_lit(ex: &ExprKind<'_>) -> Option<bool> {
159159
node: LitKind::Bool(b), ..
160160
}) = exp.kind
161161
{
162-
Some(*b)
162+
Some(b)
163163
} else {
164164
None
165165
}

clippy_lints/src/matches/match_same_arms.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_hir::{Arm, Expr, HirId, HirIdMap, HirIdMapEntry, HirIdSet, Pat, PatExp
1212
use rustc_lint::builtin::NON_EXHAUSTIVE_OMITTED_PATTERNS;
1313
use rustc_lint::{LateContext, LintContext};
1414
use rustc_middle::ty;
15-
use rustc_span::{ErrorGuaranteed, Span, Symbol};
15+
use rustc_span::{ByteSymbol, ErrorGuaranteed, Span, Symbol};
1616

1717
use super::MATCH_SAME_ARMS;
1818

@@ -193,7 +193,7 @@ enum NormalizedPat<'a> {
193193
Or(&'a [Self]),
194194
Path(Option<DefId>),
195195
LitStr(Symbol),
196-
LitBytes(&'a [u8]),
196+
LitBytes(ByteSymbol),
197197
LitInt(u128),
198198
LitBool(bool),
199199
Range(PatRange),
@@ -332,7 +332,9 @@ impl<'a> NormalizedPat<'a> {
332332
// TODO: Handle negative integers. They're currently treated as a wild match.
333333
PatExprKind::Lit { lit, negated: false } => match lit.node {
334334
LitKind::Str(sym, _) => Self::LitStr(sym),
335-
LitKind::ByteStr(ref bytes, _) | LitKind::CStr(ref bytes, _) => Self::LitBytes(bytes),
335+
LitKind::ByteStr(byte_sym, _) | LitKind::CStr(byte_sym, _) => {
336+
Self::LitBytes(byte_sym)
337+
}
336338
LitKind::Byte(val) => Self::LitInt(val.into()),
337339
LitKind::Char(val) => Self::LitInt(val.into()),
338340
LitKind::Int(val, _) => Self::LitInt(val.get()),

0 commit comments

Comments
 (0)