-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Implement concat_bytes! #87599
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
Merged
Merged
Implement concat_bytes! #87599
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
use rustc_ast as ast; | ||
use rustc_ast::{ptr::P, tokenstream::TokenStream}; | ||
use rustc_data_structures::sync::Lrc; | ||
use rustc_errors::Applicability; | ||
use rustc_expand::base::{self, DummyResult}; | ||
|
||
/// Emits errors for literal expressions that are invalid inside and outside of an array. | ||
fn invalid_type_err(cx: &mut base::ExtCtxt<'_>, expr: &P<rustc_ast::Expr>, is_nested: bool) { | ||
let lit = if let ast::ExprKind::Lit(lit) = &expr.kind { | ||
lit | ||
} else { | ||
unreachable!(); | ||
}; | ||
match lit.kind { | ||
ast::LitKind::Char(_) => { | ||
let mut err = cx.struct_span_err(expr.span, "cannot concatenate character literals"); | ||
if let Ok(snippet) = cx.sess.source_map().span_to_snippet(expr.span) { | ||
err.span_suggestion( | ||
expr.span, | ||
"try using a byte character", | ||
format!("b{}", snippet), | ||
Applicability::MachineApplicable, | ||
) | ||
.emit(); | ||
} | ||
} | ||
ast::LitKind::Str(_, _) => { | ||
let mut err = cx.struct_span_err(expr.span, "cannot concatenate string literals"); | ||
// suggestion would be invalid if we are nested | ||
if !is_nested { | ||
if let Ok(snippet) = cx.sess.source_map().span_to_snippet(expr.span) { | ||
err.span_suggestion( | ||
expr.span, | ||
"try using a byte string", | ||
format!("b{}", snippet), | ||
Applicability::MachineApplicable, | ||
); | ||
} | ||
} | ||
err.emit(); | ||
} | ||
ast::LitKind::Float(_, _) => { | ||
cx.span_err(expr.span, "cannot concatenate float literals"); | ||
} | ||
ast::LitKind::Bool(_) => { | ||
cx.span_err(expr.span, "cannot concatenate boolean literals"); | ||
} | ||
ast::LitKind::Err(_) => {} | ||
ast::LitKind::Int(_, _) if !is_nested => { | ||
let mut err = cx.struct_span_err(expr.span, "cannot concatenate numeric literals"); | ||
if let Ok(snippet) = cx.sess.source_map().span_to_snippet(expr.span) { | ||
err.span_suggestion( | ||
expr.span, | ||
"try wrapping the number in an array", | ||
format!("[{}]", snippet), | ||
Applicability::MachineApplicable, | ||
); | ||
} | ||
err.emit(); | ||
} | ||
ast::LitKind::Int( | ||
val, | ||
ast::LitIntType::Unsuffixed | ast::LitIntType::Unsigned(ast::UintTy::U8), | ||
) => { | ||
assert!(val > u8::MAX.into()); // must be an error | ||
cx.span_err(expr.span, "numeric literal is out of bounds"); | ||
} | ||
ast::LitKind::Int(_, _) => { | ||
cx.span_err(expr.span, "numeric literal is not a `u8`"); | ||
} | ||
_ => unreachable!(), | ||
} | ||
} | ||
|
||
pub fn expand_concat_bytes( | ||
cx: &mut base::ExtCtxt<'_>, | ||
sp: rustc_span::Span, | ||
tts: TokenStream, | ||
) -> Box<dyn base::MacResult + 'static> { | ||
let es = match base::get_exprs_from_tts(cx, sp, tts) { | ||
Some(e) => e, | ||
None => return DummyResult::any(sp), | ||
}; | ||
let mut accumulator = Vec::new(); | ||
let mut missing_literals = vec![]; | ||
let mut has_errors = false; | ||
for e in es { | ||
match e.kind { | ||
ast::ExprKind::Array(ref exprs) => { | ||
for expr in exprs { | ||
match expr.kind { | ||
ast::ExprKind::Array(_) => { | ||
if !has_errors { | ||
cx.span_err(expr.span, "cannot concatenate doubly nested array"); | ||
} | ||
has_errors = true; | ||
} | ||
ast::ExprKind::Lit(ref lit) => match lit.kind { | ||
ast::LitKind::Int( | ||
val, | ||
ast::LitIntType::Unsuffixed | ||
| ast::LitIntType::Unsigned(ast::UintTy::U8), | ||
) if val <= u8::MAX.into() => { | ||
accumulator.push(val as u8); | ||
} | ||
|
||
ast::LitKind::Byte(val) => { | ||
accumulator.push(val); | ||
} | ||
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") | ||
.help("try flattening the array") | ||
.emit(); | ||
} | ||
has_errors = true; | ||
} | ||
_ => { | ||
if !has_errors { | ||
invalid_type_err(cx, expr, true); | ||
} | ||
has_errors = true; | ||
} | ||
}, | ||
_ => { | ||
missing_literals.push(expr.span); | ||
} | ||
} | ||
} | ||
} | ||
ast::ExprKind::Lit(ref lit) => match lit.kind { | ||
ast::LitKind::Byte(val) => { | ||
accumulator.push(val); | ||
} | ||
ast::LitKind::ByteStr(ref bytes) => { | ||
accumulator.extend_from_slice(&bytes); | ||
} | ||
_ => { | ||
if !has_errors { | ||
invalid_type_err(cx, &e, false); | ||
} | ||
has_errors = true; | ||
} | ||
}, | ||
ast::ExprKind::Err => { | ||
has_errors = true; | ||
} | ||
_ => { | ||
missing_literals.push(e.span); | ||
} | ||
} | ||
} | ||
if !missing_literals.is_empty() { | ||
let mut err = cx.struct_span_err(missing_literals.clone(), "expected a byte literal"); | ||
err.note("only byte literals (like `b\"foo\"`, `b's'`, and `[3, 4, 5]`) can be passed to `concat_bytes!()`"); | ||
err.emit(); | ||
return base::MacEager::expr(DummyResult::raw_expr(sp, true)); | ||
} else if has_errors { | ||
return base::MacEager::expr(DummyResult::raw_expr(sp, true)); | ||
} | ||
let sp = cx.with_def_site_ctxt(sp); | ||
base::MacEager::expr(cx.expr_lit(sp, ast::LitKind::ByteStr(Lrc::from(accumulator)))) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
fn main() { | ||
let a = concat_bytes!(b'A', b"BC"); //~ ERROR use of unstable library feature 'concat_bytes' | ||
assert_eq!(a, &[65, 66, 67]); | ||
} |
12 changes: 12 additions & 0 deletions
12
src/test/ui/feature-gates/feature-gate-concat_bytes.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
error[E0658]: use of unstable library feature 'concat_bytes' | ||
--> $DIR/feature-gate-concat_bytes.rs:2:13 | ||
| | ||
LL | let a = concat_bytes!(b'A', b"BC"); | ||
| ^^^^^^^^^^^^ | ||
| | ||
= note: see issue #87555 <https://github.com/rust-lang/rust/issues/87555> for more information | ||
= help: add `#![feature(concat_bytes)]` to the crate attributes to enable | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0658`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#![feature(concat_bytes)] | ||
|
||
fn main() { | ||
concat_bytes!(pie); //~ ERROR expected a byte literal | ||
concat_bytes!(pie, pie); //~ ERROR expected a byte literal | ||
concat_bytes!("tnrsi", "tnri"); //~ ERROR cannot concatenate string literals | ||
concat_bytes!(2.8); //~ ERROR cannot concatenate float literals | ||
concat_bytes!(300); //~ ERROR cannot concatenate numeric literals | ||
concat_bytes!('a'); //~ ERROR cannot concatenate character literals | ||
concat_bytes!(true, false); //~ ERROR cannot concatenate boolean literals | ||
concat_bytes!(42, b"va", b'l'); //~ ERROR cannot concatenate numeric literals | ||
concat_bytes!(42, b"va", b'l', [1, 2]); //~ ERROR cannot concatenate numeric literals | ||
concat_bytes!([ | ||
"hi", //~ ERROR cannot concatenate string literals | ||
]); | ||
concat_bytes!([ | ||
'a', //~ ERROR cannot concatenate character literals | ||
]); | ||
concat_bytes!([ | ||
true, //~ ERROR cannot concatenate boolean literals | ||
]); | ||
concat_bytes!([ | ||
false, //~ ERROR cannot concatenate boolean literals | ||
]); | ||
concat_bytes!([ | ||
2.6, //~ ERROR cannot concatenate float literals | ||
]); | ||
concat_bytes!([ | ||
265, //~ ERROR numeric literal is out of bounds | ||
]); | ||
concat_bytes!([ | ||
-33, //~ ERROR expected a byte literal | ||
]); | ||
concat_bytes!([ | ||
b"hi!", //~ ERROR cannot concatenate doubly nested array | ||
]); | ||
concat_bytes!([ | ||
[5, 6, 7], //~ ERROR cannot concatenate doubly nested array | ||
]); | ||
concat_bytes!(5u16); //~ ERROR cannot concatenate numeric literals | ||
concat_bytes!([5u16]); //~ ERROR numeric literal is not a `u8` | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.