|
| 1 | +use rustc_ast as ast; |
| 2 | +use rustc_ast::{ptr::P, tokenstream::TokenStream}; |
| 3 | +use rustc_data_structures::sync::Lrc; |
| 4 | +use rustc_errors::Applicability; |
| 5 | +use rustc_expand::base::{self, DummyResult}; |
| 6 | + |
| 7 | +/// Emits errors for literal expressions that are invalid inside and outside of an array. |
| 8 | +fn invalid_type_err(cx: &mut base::ExtCtxt<'_>, expr: &P<rustc_ast::Expr>, is_nested: bool) { |
| 9 | + let lit = if let ast::ExprKind::Lit(lit) = &expr.kind { |
| 10 | + lit |
| 11 | + } else { |
| 12 | + unreachable!(); |
| 13 | + }; |
| 14 | + match lit.kind { |
| 15 | + ast::LitKind::Char(_) => { |
| 16 | + let mut err = cx.struct_span_err(expr.span, "cannot concatenate character literals"); |
| 17 | + if let Ok(snippet) = cx.sess.source_map().span_to_snippet(expr.span) { |
| 18 | + err.span_suggestion( |
| 19 | + expr.span, |
| 20 | + "try using a byte character", |
| 21 | + format!("b{}", snippet), |
| 22 | + Applicability::MachineApplicable, |
| 23 | + ) |
| 24 | + .emit(); |
| 25 | + } |
| 26 | + } |
| 27 | + ast::LitKind::Str(_, _) => { |
| 28 | + let mut err = cx.struct_span_err(expr.span, "cannot concatenate string literals"); |
| 29 | + // suggestion would be invalid if we are nested |
| 30 | + if !is_nested { |
| 31 | + if let Ok(snippet) = cx.sess.source_map().span_to_snippet(expr.span) { |
| 32 | + err.span_suggestion( |
| 33 | + expr.span, |
| 34 | + "try using a byte string", |
| 35 | + format!("b{}", snippet), |
| 36 | + Applicability::MachineApplicable, |
| 37 | + ); |
| 38 | + } |
| 39 | + } |
| 40 | + err.emit(); |
| 41 | + } |
| 42 | + ast::LitKind::Float(_, _) => { |
| 43 | + cx.span_err(expr.span, "cannot concatenate float literals"); |
| 44 | + } |
| 45 | + ast::LitKind::Bool(_) => { |
| 46 | + cx.span_err(expr.span, "cannot concatenate boolean literals"); |
| 47 | + } |
| 48 | + ast::LitKind::Err(_) => {} |
| 49 | + ast::LitKind::Int(_, _) if !is_nested => { |
| 50 | + let mut err = cx.struct_span_err(expr.span, "cannot concatenate numeric literals"); |
| 51 | + if let Ok(snippet) = cx.sess.source_map().span_to_snippet(expr.span) { |
| 52 | + err.span_suggestion( |
| 53 | + expr.span, |
| 54 | + "try wrapping the number in an array", |
| 55 | + format!("[{}]", snippet), |
| 56 | + Applicability::MachineApplicable, |
| 57 | + ); |
| 58 | + } |
| 59 | + err.emit(); |
| 60 | + } |
| 61 | + ast::LitKind::Int( |
| 62 | + val, |
| 63 | + ast::LitIntType::Unsuffixed | ast::LitIntType::Unsigned(ast::UintTy::U8), |
| 64 | + ) => { |
| 65 | + assert!(val > u8::MAX.into()); // must be an error |
| 66 | + cx.span_err(expr.span, "numeric literal is out of bounds"); |
| 67 | + } |
| 68 | + ast::LitKind::Int(_, _) => { |
| 69 | + cx.span_err(expr.span, "numeric literal is not a `u8`"); |
| 70 | + } |
| 71 | + _ => unreachable!(), |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +pub fn expand_concat_bytes( |
| 76 | + cx: &mut base::ExtCtxt<'_>, |
| 77 | + sp: rustc_span::Span, |
| 78 | + tts: TokenStream, |
| 79 | +) -> Box<dyn base::MacResult + 'static> { |
| 80 | + let es = match base::get_exprs_from_tts(cx, sp, tts) { |
| 81 | + Some(e) => e, |
| 82 | + None => return DummyResult::any(sp), |
| 83 | + }; |
| 84 | + let mut accumulator = Vec::new(); |
| 85 | + let mut missing_literals = vec![]; |
| 86 | + let mut has_errors = false; |
| 87 | + for e in es { |
| 88 | + match e.kind { |
| 89 | + ast::ExprKind::Array(ref exprs) => { |
| 90 | + for expr in exprs { |
| 91 | + match expr.kind { |
| 92 | + ast::ExprKind::Array(_) => { |
| 93 | + if !has_errors { |
| 94 | + cx.span_err(expr.span, "cannot concatenate doubly nested array"); |
| 95 | + } |
| 96 | + has_errors = true; |
| 97 | + } |
| 98 | + ast::ExprKind::Lit(ref lit) => match lit.kind { |
| 99 | + ast::LitKind::Int( |
| 100 | + val, |
| 101 | + ast::LitIntType::Unsuffixed |
| 102 | + | ast::LitIntType::Unsigned(ast::UintTy::U8), |
| 103 | + ) if val <= u8::MAX.into() => { |
| 104 | + accumulator.push(val as u8); |
| 105 | + } |
| 106 | + |
| 107 | + ast::LitKind::Byte(val) => { |
| 108 | + accumulator.push(val); |
| 109 | + } |
| 110 | + ast::LitKind::ByteStr(_) => { |
| 111 | + if !has_errors { |
| 112 | + cx.struct_span_err( |
| 113 | + expr.span, |
| 114 | + "cannot concatenate doubly nested array", |
| 115 | + ) |
| 116 | + .note("byte strings are treated as arrays of bytes") |
| 117 | + .help("try flattening the array") |
| 118 | + .emit(); |
| 119 | + } |
| 120 | + has_errors = true; |
| 121 | + } |
| 122 | + _ => { |
| 123 | + if !has_errors { |
| 124 | + invalid_type_err(cx, expr, true); |
| 125 | + } |
| 126 | + has_errors = true; |
| 127 | + } |
| 128 | + }, |
| 129 | + _ => { |
| 130 | + missing_literals.push(expr.span); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + ast::ExprKind::Lit(ref lit) => match lit.kind { |
| 136 | + ast::LitKind::Byte(val) => { |
| 137 | + accumulator.push(val); |
| 138 | + } |
| 139 | + ast::LitKind::ByteStr(ref bytes) => { |
| 140 | + accumulator.extend_from_slice(&bytes); |
| 141 | + } |
| 142 | + _ => { |
| 143 | + if !has_errors { |
| 144 | + invalid_type_err(cx, &e, false); |
| 145 | + } |
| 146 | + has_errors = true; |
| 147 | + } |
| 148 | + }, |
| 149 | + ast::ExprKind::Err => { |
| 150 | + has_errors = true; |
| 151 | + } |
| 152 | + _ => { |
| 153 | + missing_literals.push(e.span); |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + if !missing_literals.is_empty() { |
| 158 | + let mut err = cx.struct_span_err(missing_literals.clone(), "expected a byte literal"); |
| 159 | + err.note("only byte literals (like `b\"foo\"`, `b's'`, and `[3, 4, 5]`) can be passed to `concat_bytes!()`"); |
| 160 | + err.emit(); |
| 161 | + return base::MacEager::expr(DummyResult::raw_expr(sp, true)); |
| 162 | + } else if has_errors { |
| 163 | + return base::MacEager::expr(DummyResult::raw_expr(sp, true)); |
| 164 | + } |
| 165 | + let sp = cx.with_def_site_ctxt(sp); |
| 166 | + base::MacEager::expr(cx.expr_lit(sp, ast::LitKind::ByteStr(Lrc::from(accumulator)))) |
| 167 | +} |
0 commit comments