Skip to content

Commit 537051d

Browse files
authored
Merge pull request rust-lang#22520 from ChayimFriedman2/include-bytes-no-error
fix: Do not emit a "type annotations needed" error on `include_bytes!()` where the array length cannot be inferred
2 parents 55af177 + bb492ec commit 537051d

20 files changed

Lines changed: 144 additions & 18 deletions

File tree

crates/hir-def/src/expr_store.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,7 @@ impl ExpressionStore {
795795
visitor.on_pat(target);
796796
visitor.on_expr(value);
797797
}
798+
Expr::IncludeBytes => {}
798799
}
799800
}
800801

crates/hir-def/src/expr_store/lower.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1748,6 +1748,7 @@ impl<'db> ExprCollector<'db> {
17481748
self.alloc_expr(Expr::OffsetOf(OffsetOf { container, fields }), syntax_ptr)
17491749
}
17501750
ast::Expr::FormatArgsExpr(f) => self.collect_format_args(f, syntax_ptr),
1751+
ast::Expr::IncludeBytesExpr(_) => self.alloc_expr(Expr::IncludeBytes, syntax_ptr)
17511752
})
17521753
}
17531754

crates/hir-def/src/expr_store/pretty.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,7 @@ impl Printer<'_> {
537537
Expr::Missing => w!(self, "�"),
538538
Expr::Underscore => w!(self, "_"),
539539
Expr::InlineAsm(_) => w!(self, "builtin#asm(_)"),
540+
Expr::IncludeBytes => w!(self, "include_bytes!(_)"),
540541
Expr::OffsetOf(offset_of) => {
541542
w!(self, "builtin#offset_of(");
542543
self.print_type_ref(offset_of.container);

crates/hir-def/src/hir.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ pub enum Expr {
323323
Underscore,
324324
OffsetOf(OffsetOf),
325325
InlineAsm(InlineAsm),
326+
IncludeBytes,
326327
}
327328

328329
impl Expr {
@@ -344,7 +345,8 @@ impl Expr {
344345
| Expr::RecordLit { .. }
345346
| Expr::Tuple { .. }
346347
| Expr::OffsetOf(_)
347-
| Expr::Underscore => ExprPrecedence::Unambiguous,
348+
| Expr::Underscore
349+
| Expr::IncludeBytes => ExprPrecedence::Unambiguous,
348350

349351
Expr::Await { .. }
350352
| Expr::Call { .. }

crates/hir-expand/src/builtin/fn_macro.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -852,17 +852,9 @@ fn include_bytes_expand(
852852
span: Span,
853853
) -> ExpandResult<tt::TopSubtree> {
854854
// FIXME: actually read the file here if the user asked for macro expansion
855-
let underscore = sym::underscore;
856-
let zero = tt::Literal {
857-
text_and_suffix: sym::_0_u8,
858-
span,
859-
kind: tt::LitKind::Integer,
860-
suffix_len: 3,
861-
};
862-
// We don't use a real length since we can't know the file length, so we use an underscore
863-
// to infer it.
855+
let pound = mk_pound(span);
864856
let res = quote! {span =>
865-
&[#zero; #underscore]
857+
builtin #pound include_bytes
866858
};
867859
ExpandResult::ok(res)
868860
}

crates/hir-ty/src/infer/closure/analysis/expr_use_visitor.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,8 @@ impl<'a, 'b, 'db, D: Delegate<'db>> ExprUseVisitor<'a, 'b, 'db, D> {
692692
self.consume_expr(rhs)?;
693693
}
694694
}
695+
696+
Expr::IncludeBytes => {}
695697
}
696698
Ok(())
697699
}

crates/hir-ty/src/infer/expr.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,8 @@ impl<'db> InferenceContext<'_, 'db> {
269269
| Expr::Box { .. }
270270
| Expr::RecordLit { .. }
271271
| Expr::Yeet { .. }
272-
| Expr::Missing => false,
272+
| Expr::Missing
273+
| Expr::IncludeBytes => false,
273274
}
274275
}
275276

@@ -893,6 +894,11 @@ impl<'db> InferenceContext<'_, 'db> {
893894
self.types.types.unit
894895
}
895896
}
897+
Expr::IncludeBytes => {
898+
let len = self.table.next_const_var(Span::Dummy);
899+
let arr = Ty::new_array_with_const_len(self.interner(), self.types.types.u8, len);
900+
Ty::new_ref(self.interner(), self.types.regions.statik, arr, Mutability::Not)
901+
}
896902
};
897903
let ty = self.insert_type_vars_shallow(ty);
898904
self.write_expr_ty(tgt_expr, ty);

crates/hir-ty/src/infer/mutability.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@ impl<'db> InferenceContext<'_, 'db> {
197197
| Expr::Literal(_)
198198
| Expr::Path(_)
199199
| Expr::Continue { .. }
200-
| Expr::Underscore => (),
200+
| Expr::Underscore
201+
| Expr::IncludeBytes => (),
201202
}
202203
}
203204

crates/hir-ty/src/mir/lower.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,6 +1397,7 @@ impl<'a, 'db> MirLowerCtx<'a, 'db> {
13971397
Ok(Some(current))
13981398
}
13991399
Expr::Underscore => Ok(Some(current)),
1400+
Expr::IncludeBytes => not_supported!("include_bytes!()"),
14001401
}
14011402
}
14021403

crates/ide-db/src/syntax_helpers/node_ext.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,8 @@ pub fn for_each_tail_expr(expr: &ast::Expr, cb: &mut dyn FnMut(&ast::Expr)) {
364364
| ast::Expr::YeetExpr(_)
365365
| ast::Expr::OffsetOfExpr(_)
366366
| ast::Expr::FormatArgsExpr(_)
367-
| ast::Expr::AsmExpr(_) => cb(expr),
367+
| ast::Expr::AsmExpr(_)
368+
| ast::Expr::IncludeBytesExpr(_) => cb(expr),
368369
}
369370
}
370371

0 commit comments

Comments
 (0)