Skip to content

Commit 760ea07

Browse files
committed
cmse: more accurate span for generic arguments
1 parent 98d3864 commit 760ea07

File tree

4 files changed

+45
-64
lines changed

4 files changed

+45
-64
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/cmse.rs

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use rustc_hir::{self as hir, HirId};
44
use rustc_middle::bug;
55
use rustc_middle::ty::layout::{LayoutError, TyAndLayout};
66
use rustc_middle::ty::{self, TyCtxt, TypeVisitableExt};
7+
use rustc_span::Span;
78

89
use crate::errors;
910

@@ -20,11 +21,7 @@ pub(crate) fn validate_cmse_abi<'tcx>(
2021
match abi {
2122
ExternAbi::CmseNonSecureCall => {
2223
let hir_node = tcx.hir_node(hir_id);
23-
let hir::Node::Ty(hir::Ty {
24-
span: fn_ptr_span,
25-
kind: hir::TyKind::FnPtr(fn_ptr_ty),
26-
..
27-
}) = hir_node
24+
let hir::Node::Ty(hir::Ty { kind: hir::TyKind::FnPtr(fn_ptr_ty), .. }) = hir_node
2825
else {
2926
let span = match tcx.parent_hir_node(hir_id) {
3027
hir::Node::Item(hir::Item {
@@ -44,24 +41,24 @@ pub(crate) fn validate_cmse_abi<'tcx>(
4441
return;
4542
};
4643

47-
match is_valid_cmse_inputs(tcx, dcx, fn_sig, fn_ptr_ty.decl, abi) {
48-
Ok(()) => {}
49-
Err(layout_err) => {
50-
if should_emit_generic_error(abi, layout_err) {
51-
dcx.emit_err(errors::CmseGeneric { span: *fn_ptr_span, abi });
52-
}
44+
if let Err((span, layout_err)) =
45+
is_valid_cmse_inputs(tcx, dcx, fn_sig, fn_ptr_ty.decl, abi)
46+
{
47+
if should_emit_layout_error(abi, layout_err) {
48+
dcx.emit_err(errors::CmseGeneric { span, abi });
5349
}
5450
}
5551

5652
if let Err(layout_err) = is_valid_cmse_output(tcx, dcx, fn_sig, fn_ptr_ty.decl, abi) {
57-
if should_emit_generic_error(abi, layout_err) {
58-
dcx.emit_err(errors::CmseGeneric { span: *fn_ptr_span, abi });
53+
if should_emit_layout_error(abi, layout_err) {
54+
let span = fn_ptr_ty.decl.output.span();
55+
dcx.emit_err(errors::CmseGeneric { span, abi });
5956
}
6057
}
6158
}
6259
ExternAbi::CmseNonSecureEntry => {
6360
let hir_node = tcx.hir_node(hir_id);
64-
let Some(hir::FnSig { decl, span: fn_sig_span, .. }) = hir_node.fn_sig() else {
61+
let Some(hir::FnSig { decl, .. }) = hir_node.fn_sig() else {
6562
// might happen when this ABI is used incorrectly. That will be handled elsewhere
6663
return;
6764
};
@@ -72,18 +69,15 @@ pub(crate) fn validate_cmse_abi<'tcx>(
7269
return;
7370
}
7471

75-
match is_valid_cmse_inputs(tcx, dcx, fn_sig, decl, abi) {
76-
Ok(()) => {}
77-
Err(layout_err) => {
78-
if should_emit_generic_error(abi, layout_err) {
79-
dcx.emit_err(errors::CmseGeneric { span: *fn_sig_span, abi });
80-
}
72+
if let Err((span, layout_err)) = is_valid_cmse_inputs(tcx, dcx, fn_sig, decl, abi) {
73+
if should_emit_layout_error(abi, layout_err) {
74+
dcx.emit_err(errors::CmseGeneric { span, abi });
8175
}
8276
}
8377

8478
if let Err(layout_err) = is_valid_cmse_output(tcx, dcx, fn_sig, decl, abi) {
85-
if should_emit_generic_error(abi, layout_err) {
86-
dcx.emit_err(errors::CmseGeneric { span: *fn_sig_span, abi });
79+
if should_emit_layout_error(abi, layout_err) {
80+
dcx.emit_err(errors::CmseGeneric { span: decl.output.span(), abi });
8781
}
8882
}
8983
}
@@ -98,7 +92,7 @@ fn is_valid_cmse_inputs<'tcx>(
9892
fn_sig: ty::PolyFnSig<'tcx>,
9993
fn_decl: &hir::FnDecl<'tcx>,
10094
abi: ExternAbi,
101-
) -> Result<(), &'tcx LayoutError<'tcx>> {
95+
) -> Result<(), (Span, &'tcx LayoutError<'tcx>)> {
10296
let mut accum = 0u64;
10397
let mut excess_argument_spans = Vec::new();
10498

@@ -107,7 +101,9 @@ fn is_valid_cmse_inputs<'tcx>(
107101
let fn_sig = tcx.erase_and_anonymize_regions(fn_sig);
108102

109103
for (ty, hir_ty) in fn_sig.inputs().iter().zip(fn_decl.inputs) {
110-
let layout = tcx.layout_of(ty::TypingEnv::fully_monomorphized().as_query_input(*ty))?;
104+
let layout = tcx
105+
.layout_of(ty::TypingEnv::fully_monomorphized().as_query_input(*ty))
106+
.map_err(|e| (hir_ty.span, e))?;
111107

112108
let align = layout.layout.align().bytes();
113109
let size = layout.layout.size().bytes();
@@ -189,7 +185,7 @@ fn is_valid_cmse_output_layout<'tcx>(layout: TyAndLayout<'tcx>) -> bool {
189185
matches!(value, Primitive::Int(Integer::I64, _) | Primitive::Float(Float::F64))
190186
}
191187

192-
fn should_emit_generic_error<'tcx>(abi: ExternAbi, layout_err: &'tcx LayoutError<'tcx>) -> bool {
188+
fn should_emit_layout_error<'tcx>(abi: ExternAbi, layout_err: &'tcx LayoutError<'tcx>) -> bool {
193189
use LayoutError::*;
194190

195191
match layout_err {

tests/ui/cmse-nonsecure/cmse-nonsecure-call/generics.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,16 @@ LL | f3: extern "cmse-nonsecure-call" fn((impl Copy, u32), u32, u32, u32) ->
5555
= note: `impl Trait` is only allowed in arguments and return types of functions and methods
5656

5757
error[E0798]: generics are not allowed in `extern "cmse-nonsecure-call"` signatures
58-
--> $DIR/generics.rs:23:9
58+
--> $DIR/generics.rs:23:41
5959
|
6060
LL | f4: extern "cmse-nonsecure-call" fn(T, u32, u32, u32) -> u64,
61-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
61+
| ^
6262

6363
error[E0798]: generics are not allowed in `extern "cmse-nonsecure-call"` signatures
64-
--> $DIR/generics.rs:24:9
64+
--> $DIR/generics.rs:24:41
6565
|
6666
LL | f5: extern "cmse-nonsecure-call" fn(Wrapper<T>, u32, u32, u32) -> u64,
67-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
67+
| ^^^^^^^^^^
6868

6969
error[E0798]: return value of `"cmse-nonsecure-call"` function too large to pass via registers
7070
--> $DIR/generics.rs:30:71

tests/ui/cmse-nonsecure/cmse-nonsecure-entry/generics.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ impl<T: Copy> Wrapper<T> {
1717
}
1818

1919
extern "cmse-nonsecure-entry" fn ambient_generic_nested(
20-
//~^ ERROR [E0798]
2120
_: Wrapper<T>,
21+
//~^ ERROR [E0798]
2222
_: u32,
2323
_: u32,
2424
_: u32,
@@ -28,8 +28,8 @@ impl<T: Copy> Wrapper<T> {
2828
}
2929

3030
extern "cmse-nonsecure-entry" fn introduced_generic<U: Copy>(
31-
//~^ ERROR [E0798]
3231
_: U,
32+
//~^ ERROR [E0798]
3333
_: u32,
3434
_: u32,
3535
_: u32,
@@ -83,8 +83,8 @@ extern "cmse-nonsecure-entry" fn identity_impl_trait(v: impl Copy) -> impl Copy
8383
}
8484

8585
extern "cmse-nonsecure-entry" fn identity_impl_trait_nested(
86-
//~^ ERROR generics are not allowed in `extern "cmse-nonsecure-entry"` signatures
8786
v: (impl Copy, i32),
87+
//~^ ERROR generics are not allowed in `extern "cmse-nonsecure-entry"` signatures
8888
) -> (impl Copy, i32) {
8989
//~^ ERROR `impl Trait` is not allowed in `extern "cmse-nonsecure-entry"` signatures
9090
v

tests/ui/cmse-nonsecure/cmse-nonsecure-entry/generics.stderr

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
11
error[E0798]: generics are not allowed in `extern "cmse-nonsecure-entry"` signatures
2-
--> $DIR/generics.rs:30:1
3-
|
4-
LL | / extern "cmse-nonsecure-entry" fn introduced_generic<U: Copy>(
5-
LL | |
6-
LL | | _: U,
7-
LL | | _: u32,
8-
LL | | _: u32,
9-
LL | | _: u32,
10-
LL | | ) -> u64 {
11-
| |________^
2+
--> $DIR/generics.rs:31:8
3+
|
4+
LL | _: U,
5+
| ^
126

137
error[E0798]: generics are not allowed in `extern "cmse-nonsecure-entry"` signatures
14-
--> $DIR/generics.rs:64:1
8+
--> $DIR/generics.rs:64:48
159
|
1610
LL | extern "cmse-nonsecure-entry" fn impl_trait(_: impl Copy, _: u32, _: u32, _: u32) -> u64 {
17-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
11+
| ^^^^^^^^^
1812

1913
error[E0798]: generics are not allowed in `extern "cmse-nonsecure-entry"` signatures
20-
--> $DIR/generics.rs:79:1
14+
--> $DIR/generics.rs:79:57
2115
|
2216
LL | extern "cmse-nonsecure-entry" fn identity_impl_trait(v: impl Copy) -> impl Copy {
23-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
17+
| ^^^^^^^^^
2418

2519
error[E0798]: `impl Trait` is not allowed in `extern "cmse-nonsecure-entry"` signatures
2620
--> $DIR/generics.rs:79:71
@@ -29,13 +23,10 @@ LL | extern "cmse-nonsecure-entry" fn identity_impl_trait(v: impl Copy) -> impl
2923
| ^^^^^^^^^
3024

3125
error[E0798]: generics are not allowed in `extern "cmse-nonsecure-entry"` signatures
32-
--> $DIR/generics.rs:85:1
26+
--> $DIR/generics.rs:86:8
3327
|
34-
LL | / extern "cmse-nonsecure-entry" fn identity_impl_trait_nested(
35-
LL | |
36-
LL | | v: (impl Copy, i32),
37-
LL | | ) -> (impl Copy, i32) {
38-
| |_____________________^
28+
LL | v: (impl Copy, i32),
29+
| ^^^^^^^^^^^^^^^^
3930

4031
error[E0798]: `impl Trait` is not allowed in `extern "cmse-nonsecure-entry"` signatures
4132
--> $DIR/generics.rs:88:6
@@ -44,22 +35,16 @@ LL | ) -> (impl Copy, i32) {
4435
| ^^^^^^^^^^^^^^^^
4536

4637
error[E0798]: generics are not allowed in `extern "cmse-nonsecure-entry"` signatures
47-
--> $DIR/generics.rs:14:5
38+
--> $DIR/generics.rs:14:57
4839
|
4940
LL | extern "cmse-nonsecure-entry" fn ambient_generic(_: T, _: u32, _: u32, _: u32) -> u64 {
50-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
41+
| ^
5142

5243
error[E0798]: generics are not allowed in `extern "cmse-nonsecure-entry"` signatures
53-
--> $DIR/generics.rs:19:5
54-
|
55-
LL | / extern "cmse-nonsecure-entry" fn ambient_generic_nested(
56-
LL | |
57-
LL | | _: Wrapper<T>,
58-
LL | | _: u32,
59-
LL | | _: u32,
60-
LL | | _: u32,
61-
LL | | ) -> u64 {
62-
| |____________^
44+
--> $DIR/generics.rs:20:12
45+
|
46+
LL | _: Wrapper<T>,
47+
| ^^^^^^^^^^
6348

6449
error[E0798]: return value of `"cmse-nonsecure-entry"` function too large to pass via registers
6550
--> $DIR/generics.rs:46:65

0 commit comments

Comments
 (0)