Skip to content

Commit 7f50020

Browse files
committed
Move naked asm check into typeck
1 parent e6152cd commit 7f50020

File tree

6 files changed

+22
-44
lines changed

6 files changed

+22
-44
lines changed

compiler/rustc_hir_typeck/messages.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ hir_typeck_lossy_provenance_ptr2int =
137137
138138
hir_typeck_missing_parentheses_in_range = can't call method `{$method_name}` on type `{$ty_str}`
139139
140+
hir_typeck_naked_asm_outside_naked_fn =
141+
the `naked_asm!` macro can only be used in functions marked with `#[unsafe(naked)]`
142+
140143
hir_typeck_never_type_fallback_flowing_into_unsafe_call = never type fallback affects this call to an `unsafe` function
141144
.help = specify the type explicitly
142145
hir_typeck_never_type_fallback_flowing_into_unsafe_deref = never type fallback affects this raw pointer dereference

compiler/rustc_hir_typeck/src/errors.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,3 +983,10 @@ pub(crate) struct RegisterTypeUnstable<'a> {
983983
pub span: Span,
984984
pub ty: Ty<'a>,
985985
}
986+
987+
#[derive(Diagnostic)]
988+
#[diag(hir_typeck_naked_asm_outside_naked_fn)]
989+
pub(crate) struct NakedAsmOutsideNakedFn {
990+
#[primary_span]
991+
pub span: Span,
992+
}

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ use crate::errors::{
4444
AddressOfTemporaryTaken, BaseExpressionDoubleDot, BaseExpressionDoubleDotAddExpr,
4545
BaseExpressionDoubleDotEnableDefaultFieldValues, BaseExpressionDoubleDotRemove,
4646
CantDereference, FieldMultiplySpecifiedInInitializer, FunctionalRecordUpdateOnNonStruct,
47-
HelpUseLatestEdition, NoFieldOnType, NoFieldOnVariant, ReturnLikeStatementKind,
48-
ReturnStmtOutsideOfFnBody, StructExprNonExhaustive, TypeMismatchFruTypo,
49-
YieldExprOutsideOfCoroutine,
47+
HelpUseLatestEdition, NakedAsmOutsideNakedFn, NoFieldOnType, NoFieldOnVariant,
48+
ReturnLikeStatementKind, ReturnStmtOutsideOfFnBody, StructExprNonExhaustive,
49+
TypeMismatchFruTypo, YieldExprOutsideOfCoroutine,
5050
};
5151
use crate::{
5252
BreakableCtxt, CoroutineTypes, Diverges, FnCtxt, GatherLocalsVisitor, Needs,
@@ -524,7 +524,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
524524
ExprKind::InlineAsm(asm) => {
525525
// We defer some asm checks as we may not have resolved the input and output types yet (they may still be infer vars).
526526
self.deferred_asm_checks.borrow_mut().push((asm, expr.hir_id));
527-
self.check_expr_asm(asm)
527+
self.check_expr_asm(asm, expr.span)
528528
}
529529
ExprKind::OffsetOf(container, fields) => {
530530
self.check_expr_offset_of(container, fields, expr)
@@ -3761,7 +3761,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
37613761
}
37623762
}
37633763

3764-
fn check_expr_asm(&self, asm: &'tcx hir::InlineAsm<'tcx>) -> Ty<'tcx> {
3764+
fn check_expr_asm(&self, asm: &'tcx hir::InlineAsm<'tcx>, span: Span) -> Ty<'tcx> {
3765+
if let rustc_ast::AsmMacro::NakedAsm = asm.asm_macro {
3766+
if !self.tcx.has_attr(self.body_id, sym::naked) {
3767+
self.tcx.dcx().emit_err(NakedAsmOutsideNakedFn { span });
3768+
}
3769+
}
3770+
37653771
let mut diverge = asm.asm_macro.diverges(asm.options);
37663772

37673773
for (op, _op_sp) in asm.operands {

compiler/rustc_passes/messages.ftl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -509,9 +509,6 @@ passes_must_not_suspend =
509509
passes_must_use_no_effect =
510510
`#[must_use]` has no effect when applied to {$article} {$target}
511511
512-
passes_naked_asm_outside_naked_fn =
513-
the `naked_asm!` macro can only be used in functions marked with `#[unsafe(naked)]`
514-
515512
passes_naked_functions_asm_block =
516513
naked functions must contain a single `naked_asm!` invocation
517514
.label_multiple_asm = multiple `naked_asm!` invocations are not allowed in naked functions

compiler/rustc_passes/src/errors.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,13 +1252,6 @@ pub(crate) struct NakedFunctionIncompatibleAttribute {
12521252
pub attr: String,
12531253
}
12541254

1255-
#[derive(Diagnostic)]
1256-
#[diag(passes_naked_asm_outside_naked_fn)]
1257-
pub(crate) struct NakedAsmOutsideNakedFn {
1258-
#[primary_span]
1259-
pub span: Span,
1260-
}
1261-
12621255
#[derive(Diagnostic)]
12631256
#[diag(passes_attr_only_in_functions)]
12641257
pub(crate) struct AttrOnlyInFunctions {

compiler/rustc_passes/src/naked_functions.rs

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@ use rustc_hir::def::DefKind;
55
use rustc_hir::def_id::{LocalDefId, LocalModDefId};
66
use rustc_hir::intravisit::Visitor;
77
use rustc_hir::{ExprKind, HirIdSet, StmtKind};
8-
use rustc_middle::hir::nested_filter::OnlyBodies;
98
use rustc_middle::query::Providers;
109
use rustc_middle::span_bug;
1110
use rustc_middle::ty::TyCtxt;
1211
use rustc_span::{Span, sym};
1312

1413
use crate::errors::{
15-
NakedAsmOutsideNakedFn, NakedFunctionsAsmBlock, NakedFunctionsMustNakedAsm, NoPatterns,
16-
ParamsNotAllowed,
14+
NakedFunctionsAsmBlock, NakedFunctionsMustNakedAsm, NoPatterns, ParamsNotAllowed,
1715
};
1816

1917
pub(crate) fn provide(providers: &mut Providers) {
@@ -45,10 +43,6 @@ fn check_mod_naked_functions(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) {
4543
check_no_patterns(tcx, body.params);
4644
check_no_parameters_use(tcx, body);
4745
check_asm(tcx, def_id, body);
48-
} else {
49-
// `naked_asm!` is not allowed outside of functions marked as `#[naked]`
50-
let mut visitor = CheckNakedAsmInNakedFn { tcx };
51-
visitor.visit_body(body);
5246
}
5347
}
5448
}
@@ -231,25 +225,3 @@ impl<'tcx> Visitor<'tcx> for CheckInlineAssembly {
231225
self.check_expr(expr, expr.span);
232226
}
233227
}
234-
235-
struct CheckNakedAsmInNakedFn<'tcx> {
236-
tcx: TyCtxt<'tcx>,
237-
}
238-
239-
impl<'tcx> Visitor<'tcx> for CheckNakedAsmInNakedFn<'tcx> {
240-
type NestedFilter = OnlyBodies;
241-
242-
fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
243-
self.tcx
244-
}
245-
246-
fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
247-
if let ExprKind::InlineAsm(inline_asm) = expr.kind {
248-
if let rustc_ast::AsmMacro::NakedAsm = inline_asm.asm_macro {
249-
self.tcx.dcx().emit_err(NakedAsmOutsideNakedFn { span: expr.span });
250-
}
251-
}
252-
253-
hir::intravisit::walk_expr(self, expr);
254-
}
255-
}

0 commit comments

Comments
 (0)