Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup of 7 pull requests #105492

Closed
wants to merge 40 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
4ced370
Make `missing_copy_implementations` more cautious
Sep 28, 2022
b209ff2
Update trait check
Nov 19, 2022
34277fc
Rebase
Nov 29, 2022
e658144
Rename `LitKind::to_token_lit` as `LitKind::synthesize_token_lit`.
nnethercote Nov 29, 2022
a7f35c4
Add `StrStyle` to `ast::LitKind::ByteStr`.
nnethercote Nov 29, 2022
2fd364a
Remove `token::Lit` from `ast::MetaItemLit`.
nnethercote Nov 29, 2022
d5526ff
Reorder `StrLit` fields.
nnethercote Nov 29, 2022
4a4270a
Migrate "function cannot return without recursing" diagnostic
AsyaTheAbove Aug 20, 2022
6b000cb
Migrate "unsafe_op_in_unsafe_fn" lints
AsyaTheAbove Aug 20, 2022
1443d1b
Migrate "requires unsafe" diagnostics
AsyaTheAbove Aug 24, 2022
cc85b01
Migrate "unused unsafe" lint
AsyaTheAbove Aug 24, 2022
07d3ce5
Migrate "non-exhaustive patterns: type is non-empty" diagnostic
AsyaTheAbove Aug 26, 2022
85c6818
Migrate pattern inlining error diagnostics
AsyaTheAbove Aug 26, 2022
7d0d2de
Migrate unreachable pattern diagnostic
AsyaTheAbove Aug 27, 2022
75694d5
Migrate "constant pattern depends on generic parameter" diagnostic
AsyaTheAbove Aug 27, 2022
baa6c15
Migrate "could not evaluate const pattern" diagnostic
AsyaTheAbove Aug 27, 2022
5c64edf
Migrate lower range bound diagnostics
AsyaTheAbove Aug 27, 2022
42a8f37
Migrate leading/trailing irrefutable let pattern diagnostics
AsyaTheAbove Aug 28, 2022
8c00282
Migrate pattern bindings with variant name lint
AsyaTheAbove Aug 29, 2022
1a18ee8
Migrate irrefutable let pattern diagnostics
AsyaTheAbove Aug 29, 2022
e2d9380
Migrate borrow of moved value diagnostic
AsyaTheAbove Aug 30, 2022
03128ba
Migrate multiple mut borrows diagnostic
AsyaTheAbove Aug 30, 2022
1d324dd
Resolve various merge conflicts
Nov 26, 2022
7d30472
Remove `mk_name_value_item{,_str}`.
nnethercote Dec 5, 2022
4ae956f
Remove `ExtCtxt::expr_lit`.
nnethercote Dec 5, 2022
d887615
Parameterise `Parser::{recover_unclosed_char,handle_missing_lit}`.
nnethercote Dec 5, 2022
568e647
Remove three uses of `LitKind::synthesize_token_lit`.
nnethercote Dec 5, 2022
7e0c6db
Remove `LitKind::synthesize_token_lit`.
nnethercote Dec 5, 2022
58e60ac
Make `VecDeque::from_iter` O(1) from `vec(_deque)::IntoIter`
scottmcm Dec 8, 2022
5626df9
Add `rustc_on_unimplemented` to `Sum` and `Product` trait.
aDotInTheVoid Dec 4, 2022
90da11d
rustdoc: remove no-op mobile CSS `#sidebar-toggle { text-align }`
notriddle Dec 9, 2022
6648134
Apply review feedback; Fix no_global_oom_handling build
scottmcm Dec 9, 2022
f41576b
Fix typo in apple_base.rs
eltociear Dec 9, 2022
0f0b4f6
Rollup merge of #102406 - mejrs:missing_copy, r=wesleywiser
Dylan-DPC Dec 9, 2022
979eaf4
Rollup merge of #104417 - mejrs:mir_build, r=davidtwco
Dylan-DPC Dec 9, 2022
c07a66a
Rollup merge of #105160 - nnethercote:rm-Lit-token_lit, r=petrochenkov
Dylan-DPC Dec 9, 2022
bd92c94
Rollup merge of #105265 - aDotInTheVoid:sum-product-on-unimplemented,…
Dylan-DPC Dec 9, 2022
f5aaea5
Rollup merge of #105453 - scottmcm:vecdeque_from_iter, r=the8472
Dylan-DPC Dec 9, 2022
e2b4035
Rollup merge of #105480 - notriddle:notriddle/sidebar-toggle-mobile-c…
Dylan-DPC Dec 9, 2022
3b7e614
Rollup merge of #105489 - eltociear:patch-17, r=Dylan-DPC
Dylan-DPC Dec 9, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4070,6 +4070,7 @@ dependencies = [
"rustc_hir",
"rustc_index",
"rustc_infer",
"rustc_macros",
"rustc_middle",
"rustc_serialize",
"rustc_session",
Expand Down
24 changes: 14 additions & 10 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1735,8 +1735,10 @@ pub enum StrStyle {
/// A literal in a meta item.
#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
pub struct MetaItemLit {
/// The original literal token as written in source code.
pub token_lit: token::Lit,
/// The original literal as written in the source code.
pub symbol: Symbol,
/// The original suffix as written in the source code.
pub suffix: Option<Symbol>,
/// The "semantic" representation of the literal lowered from the original tokens.
/// Strings are unescaped, hexadecimal forms are eliminated, etc.
pub kind: LitKind,
Expand All @@ -1746,13 +1748,14 @@ pub struct MetaItemLit {
/// Similar to `MetaItemLit`, but restricted to string literals.
#[derive(Clone, Copy, Encodable, Decodable, Debug)]
pub struct StrLit {
/// The original literal token as written in source code.
pub style: StrStyle,
/// The original literal as written in source code.
pub symbol: Symbol,
/// The original suffix as written in source code.
pub suffix: Option<Symbol>,
pub span: Span,
/// The unescaped "semantic" representation of the literal lowered from the original token.
/// The semantic (unescaped) representation of the literal.
pub symbol_unescaped: Symbol,
pub style: StrStyle,
pub span: Span,
}

impl StrLit {
Expand Down Expand Up @@ -1798,8 +1801,9 @@ pub enum LitKind {
/// A string literal (`"foo"`). The symbol is unescaped, and so may differ
/// from the original token's symbol.
Str(Symbol, StrStyle),
/// A byte string (`b"foo"`).
ByteStr(Lrc<[u8]>),
/// A byte string (`b"foo"`). Not stored as a symbol because it might be
/// non-utf8, and symbols only allow utf8 strings.
ByteStr(Lrc<[u8]>, StrStyle),
/// A byte char (`b'f'`).
Byte(u8),
/// A character literal (`'a'`).
Expand All @@ -1824,7 +1828,7 @@ impl LitKind {

/// Returns `true` if this literal is byte literal string.
pub fn is_bytestr(&self) -> bool {
matches!(self, LitKind::ByteStr(_))
matches!(self, LitKind::ByteStr(..))
}

/// Returns `true` if this is a numeric literal.
Expand Down Expand Up @@ -3101,7 +3105,7 @@ mod size_asserts {
static_assert_size!(ItemKind, 112);
static_assert_size!(LitKind, 24);
static_assert_size!(Local, 72);
static_assert_size!(MetaItemLit, 48);
static_assert_size!(MetaItemLit, 40);
static_assert_size!(Param, 40);
static_assert_size!(Pat, 88);
static_assert_size!(Path, 24);
Expand Down
18 changes: 3 additions & 15 deletions compiler/rustc_ast/src/attr/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
//! Functions dealing with attributes and meta items.

use crate::ast;
use crate::ast::{AttrArgs, AttrArgsEq, AttrId, AttrItem, AttrKind, AttrStyle, AttrVec, Attribute};
use crate::ast::{DelimArgs, Expr, ExprKind, LitKind, MetaItemLit};
use crate::ast::{MacDelimiter, MetaItem, MetaItemKind, NestedMetaItem, NormalAttr};
use crate::ast::{Path, PathSegment, StrStyle, DUMMY_NODE_ID};
use crate::ast::{Path, PathSegment, DUMMY_NODE_ID};
use crate::ptr::P;
use crate::token::{self, CommentKind, Delimiter, Token};
use crate::tokenstream::{DelimSpan, Spacing, TokenTree};
use crate::tokenstream::{LazyAttrTokenStream, TokenStream};
use crate::util::comments;
use crate::util::literal::escape_string_symbol;
use rustc_data_structures::sync::WorkerLocal;
use rustc_index::bit_set::GrowableBitSet;
use rustc_span::symbol::{sym, Ident, Symbol};
Expand Down Expand Up @@ -321,18 +321,6 @@ impl Attribute {
}
}

/* Constructors */

pub fn mk_name_value_item_str(ident: Ident, str: Symbol, str_span: Span) -> MetaItem {
mk_name_value_item(ident, LitKind::Str(str, ast::StrStyle::Cooked), str_span)
}

pub fn mk_name_value_item(ident: Ident, kind: LitKind, lit_span: Span) -> MetaItem {
let lit = MetaItemLit { token_lit: kind.to_token_lit(), kind, span: lit_span };
let span = ident.span.to(lit_span);
MetaItem { path: Path::from_ident(ident), kind: MetaItemKind::NameValue(lit), span }
}

pub struct AttrIdGenerator(WorkerLocal<Cell<u32>>);

#[cfg(debug_assertions)]
Expand Down Expand Up @@ -408,7 +396,7 @@ pub fn mk_attr_name_value_str(
val: Symbol,
span: Span,
) -> Attribute {
let lit = LitKind::Str(val, StrStyle::Cooked).to_token_lit();
let lit = token::Lit::new(token::Str, escape_string_symbol(val), None);
let expr = P(Expr {
id: DUMMY_NODE_ID,
kind: ExprKind::Lit(lit),
Expand Down
142 changes: 94 additions & 48 deletions compiler/rustc_ast/src/util/literal.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
//! Code related to parsing literals.

use crate::ast::{self, LitKind, MetaItemLit};
use crate::ast::{self, LitKind, MetaItemLit, StrStyle};
use crate::token::{self, Token};
use rustc_lexer::unescape::{byte_from_char, unescape_byte, unescape_char, unescape_literal, Mode};
use rustc_span::symbol::{kw, sym, Symbol};
use rustc_span::Span;
use std::ascii;
use std::{ascii, fmt, str};

// Escapes a string, represented as a symbol. Reuses the original symbol,
// avoiding interning, if no changes are required.
pub fn escape_string_symbol(symbol: Symbol) -> Symbol {
let s = symbol.as_str();
let escaped = s.escape_default().to_string();
if s == escaped { symbol } else { Symbol::intern(&escaped) }
}

// Escapes a char.
pub fn escape_char_symbol(ch: char) -> Symbol {
let s: String = ch.escape_default().map(Into::<char>::into).collect();
Symbol::intern(&s)
}

// Escapes a byte string.
pub fn escape_byte_str_symbol(bytes: &[u8]) -> Symbol {
let s = bytes.escape_ascii().to_string();
Symbol::intern(&s)
}

#[derive(Debug)]
pub enum LitError {
Expand Down Expand Up @@ -115,9 +135,9 @@ impl LitKind {
}
});
error?;
LitKind::ByteStr(buf.into())
LitKind::ByteStr(buf.into(), StrStyle::Cooked)
}
token::ByteStrRaw(_) => {
token::ByteStrRaw(n) => {
let s = symbol.as_str();
let bytes = if s.contains('\r') {
let mut buf = Vec::with_capacity(s.len());
Expand All @@ -136,69 +156,95 @@ impl LitKind {
symbol.to_string().into_bytes()
};

LitKind::ByteStr(bytes.into())
LitKind::ByteStr(bytes.into(), StrStyle::Raw(n))
}
token::Err => LitKind::Err,
})
}
}

/// Attempts to recover a token from semantic literal.
/// This function is used when the original token doesn't exist (e.g. the literal is created
/// by an AST-based macro) or unavailable (e.g. from HIR pretty-printing).
pub fn to_token_lit(&self) -> token::Lit {
let (kind, symbol, suffix) = match *self {
LitKind::Str(symbol, ast::StrStyle::Cooked) => {
// Don't re-intern unless the escaped string is different.
let s = symbol.as_str();
let escaped = s.escape_default().to_string();
let symbol = if s == escaped { symbol } else { Symbol::intern(&escaped) };
(token::Str, symbol, None)
}
LitKind::Str(symbol, ast::StrStyle::Raw(n)) => (token::StrRaw(n), symbol, None),
LitKind::ByteStr(ref bytes) => {
let string = bytes.escape_ascii().to_string();
(token::ByteStr, Symbol::intern(&string), None)
impl fmt::Display for LitKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
LitKind::Byte(b) => {
let b: String = ascii::escape_default(b).map(Into::<char>::into).collect();
write!(f, "b'{}'", b)?;
}
LitKind::Byte(byte) => {
let string: String = ascii::escape_default(byte).map(Into::<char>::into).collect();
(token::Byte, Symbol::intern(&string), None)
LitKind::Char(ch) => write!(f, "'{}'", escape_char_symbol(ch))?,
LitKind::Str(sym, StrStyle::Cooked) => write!(f, "\"{}\"", escape_string_symbol(sym))?,
LitKind::Str(sym, StrStyle::Raw(n)) => write!(
f,
"r{delim}\"{string}\"{delim}",
delim = "#".repeat(n as usize),
string = sym
)?,
LitKind::ByteStr(ref bytes, StrStyle::Cooked) => {
write!(f, "b\"{}\"", escape_byte_str_symbol(bytes))?
}
LitKind::Char(ch) => {
let string: String = ch.escape_default().map(Into::<char>::into).collect();
(token::Char, Symbol::intern(&string), None)
LitKind::ByteStr(ref bytes, StrStyle::Raw(n)) => {
// Unwrap because raw byte string literals can only contain ASCII.
let symbol = str::from_utf8(bytes).unwrap();
write!(
f,
"br{delim}\"{string}\"{delim}",
delim = "#".repeat(n as usize),
string = symbol
)?;
}
LitKind::Int(n, ty) => {
let suffix = match ty {
ast::LitIntType::Unsigned(ty) => Some(ty.name()),
ast::LitIntType::Signed(ty) => Some(ty.name()),
ast::LitIntType::Unsuffixed => None,
};
(token::Integer, sym::integer(n), suffix)
write!(f, "{}", n)?;
match ty {
ast::LitIntType::Unsigned(ty) => write!(f, "{}", ty.name())?,
ast::LitIntType::Signed(ty) => write!(f, "{}", ty.name())?,
ast::LitIntType::Unsuffixed => {}
}
}
LitKind::Float(symbol, ty) => {
let suffix = match ty {
ast::LitFloatType::Suffixed(ty) => Some(ty.name()),
ast::LitFloatType::Unsuffixed => None,
};
(token::Float, symbol, suffix)
write!(f, "{}", symbol)?;
match ty {
ast::LitFloatType::Suffixed(ty) => write!(f, "{}", ty.name())?,
ast::LitFloatType::Unsuffixed => {}
}
}
LitKind::Bool(value) => {
let symbol = if value { kw::True } else { kw::False };
(token::Bool, symbol, None)
LitKind::Bool(b) => write!(f, "{}", if b { "true" } else { "false" })?,
LitKind::Err => {
// This only shows up in places like `-Zunpretty=hir` output, so we
// don't bother to produce something useful.
write!(f, "<bad-literal>")?;
}
// This only shows up in places like `-Zunpretty=hir` output, so we
// don't bother to produce something useful.
LitKind::Err => (token::Err, Symbol::intern("<bad-literal>"), None),
};
}

token::Lit::new(kind, symbol, suffix)
Ok(())
}
}

impl MetaItemLit {
/// Converts token literal into a meta item literal.
/// Converts a token literal into a meta item literal.
pub fn from_token_lit(token_lit: token::Lit, span: Span) -> Result<MetaItemLit, LitError> {
Ok(MetaItemLit { token_lit, kind: LitKind::from_token_lit(token_lit)?, span })
Ok(MetaItemLit {
symbol: token_lit.symbol,
suffix: token_lit.suffix,
kind: LitKind::from_token_lit(token_lit)?,
span,
})
}

/// Cheaply converts a meta item literal into a token literal.
pub fn as_token_lit(&self) -> token::Lit {
let kind = match self.kind {
LitKind::Bool(_) => token::Bool,
LitKind::Str(_, ast::StrStyle::Cooked) => token::Str,
LitKind::Str(_, ast::StrStyle::Raw(n)) => token::StrRaw(n),
LitKind::ByteStr(_, ast::StrStyle::Cooked) => token::ByteStr,
LitKind::ByteStr(_, ast::StrStyle::Raw(n)) => token::ByteStrRaw(n),
LitKind::Byte(_) => token::Byte,
LitKind::Char(_) => token::Char,
LitKind::Int(..) => token::Integer,
LitKind::Float(..) => token::Float,
LitKind::Err => token::Err,
};

token::Lit::new(kind, self.symbol, self.suffix)
}

/// Converts an arbitrary token into meta item literal.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
ExprKind::IncludedBytes(bytes) => hir::ExprKind::Lit(respan(
self.lower_span(e.span),
LitKind::ByteStr(bytes.clone()),
LitKind::ByteStr(bytes.clone(), StrStyle::Cooked),
)),
ExprKind::Cast(expr, ty) => {
let expr = self.lower_expr(expr);
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
lit
} else {
MetaItemLit {
token_lit: token::Lit::new(token::LitKind::Err, kw::Empty, None),
symbol: kw::Empty,
suffix: None,
kind: LitKind::Err,
span: DUMMY_SP,
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_pretty/src/pprust/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
}

fn print_meta_item_lit(&mut self, lit: &ast::MetaItemLit) {
self.print_token_literal(lit.token_lit, lit.span)
self.print_token_literal(lit.as_token_lit(), lit.span)
}

fn print_token_literal(&mut self, token_lit: token::Lit, span: Span) {
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_ast_pretty/src/pprust/state/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use crate::pp::Breaks::Inconsistent;
use crate::pprust::state::{AnnNode, IterDelimited, PrintState, State, INDENT_UNIT};

use rustc_ast::ptr::P;
use rustc_ast::token;
use rustc_ast::util::literal::escape_byte_str_symbol;
use rustc_ast::util::parser::{self, AssocOp, Fixity};
use rustc_ast::{self as ast, BlockCheckMode};

Expand Down Expand Up @@ -323,7 +325,7 @@ impl<'a> State<'a> {
self.print_token_literal(*token_lit, expr.span);
}
ast::ExprKind::IncludedBytes(bytes) => {
let lit = ast::LitKind::ByteStr(bytes.clone()).to_token_lit();
let lit = token::Lit::new(token::ByteStr, escape_byte_str_symbol(bytes), None);
self.print_token_literal(lit, expr.span)
}
ast::ExprKind::Cast(expr, ty) => {
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_builtin_macros/src/concat_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fn invalid_type_err(
Ok(ast::LitKind::Int(_, _)) => {
cx.span_err(span, "numeric literal is not a `u8`");
}
Ok(ast::LitKind::ByteStr(_) | ast::LitKind::Byte(_)) => unreachable!(),
Ok(ast::LitKind::ByteStr(..) | ast::LitKind::Byte(_)) => unreachable!(),
Err(err) => {
report_lit_error(&cx.sess.parse_sess, err, token_lit, span);
}
Expand Down Expand Up @@ -97,7 +97,7 @@ fn handle_array_element(
)) if val <= u8::MAX.into() => Some(val as u8),

Ok(ast::LitKind::Byte(val)) => Some(val),
Ok(ast::LitKind::ByteStr(_)) => {
Ok(ast::LitKind::ByteStr(..)) => {
if !*has_errors {
cx.struct_span_err(expr.span, "cannot concatenate doubly nested array")
.note("byte strings are treated as arrays of bytes")
Expand Down Expand Up @@ -174,7 +174,7 @@ pub fn expand_concat_bytes(
Ok(ast::LitKind::Byte(val)) => {
accumulator.push(val);
}
Ok(ast::LitKind::ByteStr(ref bytes)) => {
Ok(ast::LitKind::ByteStr(ref bytes, _)) => {
accumulator.extend_from_slice(&bytes);
}
_ => {
Expand Down
10 changes: 6 additions & 4 deletions compiler/rustc_builtin_macros/src/derive.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::cfg_eval::cfg_eval;

use rustc_ast as ast;
use rustc_ast::{token, GenericParamKind, ItemKind, MetaItemKind, NestedMetaItem, StmtKind};
use rustc_ast::{GenericParamKind, ItemKind, MetaItemKind, NestedMetaItem, StmtKind};
use rustc_errors::{struct_span_err, Applicability};
use rustc_expand::base::{Annotatable, ExpandResult, ExtCtxt, Indeterminate, MultiItemModifier};
use rustc_feature::AttributeTemplate;
Expand Down Expand Up @@ -130,9 +130,11 @@ fn report_bad_target(sess: &Session, item: &Annotatable, span: Span) -> bool {
}

fn report_unexpected_meta_item_lit(sess: &Session, lit: &ast::MetaItemLit) {
let help_msg = match lit.token_lit.kind {
token::Str if rustc_lexer::is_ident(lit.token_lit.symbol.as_str()) => {
format!("try using `#[derive({})]`", lit.token_lit.symbol)
let help_msg = match lit.kind {
ast::LitKind::Str(_, ast::StrStyle::Cooked)
if rustc_lexer::is_ident(lit.symbol.as_str()) =>
{
format!("try using `#[derive({})]`", lit.symbol)
}
_ => "for example, write `#[derive(Debug)]` for `Debug`".to_string(),
};
Expand Down
Loading