Skip to content

Commit 5c1e011

Browse files
authored
Rollup merge of #77560 - rschoon:fix-litkind-rc-bytebuf, r=lcnr
Fix LitKind's byte buffer to use refcounted slice While working on adding a new lint for clippy (see rust-lang/rust-clippy#6044) for avoiding shared ownership of "mutable buffer" types (such as using `Rc<Vec<T>>` instead of `Rc<[T]>`), I noticed a type exported from rustc_ast and used by clippy gets caught by the lint. This PR fixes the exported type. This PR includes the actual change to clippy too, but I will open a PR directly against clippy for that part (although it will currently fail to build there).
2 parents 2970af8 + 62f7712 commit 5c1e011

File tree

5 files changed

+6
-9
lines changed

5 files changed

+6
-9
lines changed

compiler/rustc_ast/src/ast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1606,7 +1606,7 @@ pub enum LitKind {
16061606
/// A string literal (`"foo"`).
16071607
Str(Symbol, StrStyle),
16081608
/// A byte string (`b"foo"`).
1609-
ByteStr(Lrc<Vec<u8>>),
1609+
ByteStr(Lrc<[u8]>),
16101610
/// A byte char (`b'f'`).
16111611
Byte(u8),
16121612
/// A character literal (`'a'`).

compiler/rustc_ast/src/util/literal.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use crate::ast::{self, Lit, LitKind};
44
use crate::token::{self, Token};
55
use crate::tokenstream::TokenTree;
66

7-
use rustc_data_structures::sync::Lrc;
87
use rustc_lexer::unescape::{unescape_byte, unescape_char};
98
use rustc_lexer::unescape::{unescape_byte_literal, unescape_literal, Mode};
109
use rustc_span::symbol::{kw, sym, Symbol};
@@ -108,7 +107,7 @@ impl LitKind {
108107
});
109108
error?;
110109
buf.shrink_to_fit();
111-
LitKind::ByteStr(Lrc::new(buf))
110+
LitKind::ByteStr(buf.into())
112111
}
113112
token::ByteStrRaw(_) => {
114113
let s = symbol.as_str();
@@ -128,7 +127,7 @@ impl LitKind {
128127
symbol.to_string().into_bytes()
129128
};
130129

131-
LitKind::ByteStr(Lrc::new(bytes))
130+
LitKind::ByteStr(bytes.into())
132131
}
133132
token::Err => LitKind::Err(symbol),
134133
})

compiler/rustc_builtin_macros/src/source_util.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ use rustc_span::{self, Pos, Span};
1313
use smallvec::SmallVec;
1414
use std::rc::Rc;
1515

16-
use rustc_data_structures::sync::Lrc;
17-
1816
// These macros all relate to the file system; they either return
1917
// the column/row/filename of the expression, or they include
2018
// a given file into the current one.
@@ -216,7 +214,7 @@ pub fn expand_include_bytes(
216214
}
217215
};
218216
match cx.source_map().load_binary_file(&file) {
219-
Ok(bytes) => base::MacEager::expr(cx.expr_lit(sp, ast::LitKind::ByteStr(Lrc::new(bytes)))),
217+
Ok(bytes) => base::MacEager::expr(cx.expr_lit(sp, ast::LitKind::ByteStr(bytes.into()))),
220218
Err(e) => {
221219
cx.span_err(sp, &format!("couldn't read {}: {}", file.display(), e));
222220
DummyResult::any(sp)

compiler/rustc_mir_build/src/thir/constant.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ crate fn lit_to_const<'tcx>(
3131
(ast::LitKind::ByteStr(data), ty::Ref(_, inner_ty, _))
3232
if matches!(inner_ty.kind(), ty::Slice(_)) =>
3333
{
34-
let allocation = Allocation::from_byte_aligned_bytes(data as &Vec<u8>);
34+
let allocation = Allocation::from_byte_aligned_bytes(data as &[u8]);
3535
let allocation = tcx.intern_const_alloc(allocation);
3636
ConstValue::Slice { data: allocation, start: 0, end: data.len() }
3737
}

src/tools/clippy/clippy_lints/src/consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ pub fn lit_to_constant(lit: &LitKind, ty: Option<Ty<'_>>) -> Constant {
155155
match *lit {
156156
LitKind::Str(ref is, _) => Constant::Str(is.to_string()),
157157
LitKind::Byte(b) => Constant::Int(u128::from(b)),
158-
LitKind::ByteStr(ref s) => Constant::Binary(Lrc::from(s.as_slice())),
158+
LitKind::ByteStr(ref s) => Constant::Binary(Lrc::clone(s)),
159159
LitKind::Char(c) => Constant::Char(c),
160160
LitKind::Int(n, _) => Constant::Int(n),
161161
LitKind::Float(ref is, LitFloatType::Suffixed(fty)) => match fty {

0 commit comments

Comments
 (0)