Skip to content

Commit 449a440

Browse files
committed
test attr: point at return type if Termination bound unsatisfied
1 parent 9be2f35 commit 449a440

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

compiler/rustc_builtin_macros/src/test.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ pub fn expand_test_or_bench(
103103
};
104104

105105
// Note: non-associated fn items are already handled by `expand_test_or_bench`
106-
if !matches!(item.kind, ast::ItemKind::Fn(_)) {
106+
let ast::ItemKind::Fn(fn_) = &item.kind else {
107107
let diag = &cx.sess.parse_sess.span_diagnostic;
108108
let msg = "the `#[test]` attribute may only be used on a non-associated function";
109109
let mut err = match item.kind {
@@ -121,7 +121,7 @@ pub fn expand_test_or_bench(
121121
.emit();
122122

123123
return vec![Annotatable::Item(item)];
124-
}
124+
};
125125

126126
// has_*_signature will report any errors in the type so compilation
127127
// will fail. We shouldn't try to expand in this case because the errors
@@ -132,12 +132,14 @@ pub fn expand_test_or_bench(
132132
return vec![Annotatable::Item(item)];
133133
}
134134

135-
let (sp, attr_sp) = (cx.with_def_site_ctxt(item.span), cx.with_def_site_ctxt(attr_sp));
135+
let sp = cx.with_def_site_ctxt(item.span);
136+
let ret_ty_sp = cx.with_def_site_ctxt(fn_.sig.decl.output.span());
137+
let attr_sp = cx.with_def_site_ctxt(attr_sp);
136138

137139
let test_id = Ident::new(sym::test, attr_sp);
138140

139141
// creates test::$name
140-
let test_path = |name| cx.path(sp, vec![test_id, Ident::from_str_and_span(name, sp)]);
142+
let test_path = |name| cx.path(ret_ty_sp, vec![test_id, Ident::from_str_and_span(name, sp)]);
141143

142144
// creates test::ShouldPanic::$name
143145
let should_panic_path = |name| {
@@ -183,7 +185,7 @@ pub fn expand_test_or_bench(
183185
vec![
184186
// super::$test_fn(b)
185187
cx.expr_call(
186-
sp,
188+
ret_ty_sp,
187189
cx.expr_path(cx.path(sp, vec![item.ident])),
188190
vec![cx.expr_ident(sp, b)],
189191
),
@@ -207,7 +209,11 @@ pub fn expand_test_or_bench(
207209
cx.expr_path(test_path("assert_test_result")),
208210
vec![
209211
// $test_fn()
210-
cx.expr_call(sp, cx.expr_path(cx.path(sp, vec![item.ident])), vec![]), // )
212+
cx.expr_call(
213+
ret_ty_sp,
214+
cx.expr_path(cx.path(sp, vec![item.ident])),
215+
vec![],
216+
), // )
211217
],
212218
), // }
213219
), // )

src/test/ui/rfc-1937-termination-trait/termination-trait-test-wrong-type.stderr

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
error[E0277]: the trait bound `f32: Termination` is not satisfied
2-
--> $DIR/termination-trait-test-wrong-type.rs:6:1
2+
--> $DIR/termination-trait-test-wrong-type.rs:6:31
33
|
4-
LL | #[test]
5-
| ------- in this procedural macro expansion
6-
LL | / fn can_parse_zero_as_f32() -> Result<f32, ParseFloatError> {
7-
LL | | "0".parse()
8-
LL | | }
9-
| |_^ the trait `Termination` is not implemented for `f32`
4+
LL | #[test]
5+
| ------- in this procedural macro expansion
6+
LL | fn can_parse_zero_as_f32() -> Result<f32, ParseFloatError> {
7+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Termination` is not implemented for `f32`
108
|
119
= note: required for `Result<f32, ParseFloatError>` to implement `Termination`
1210
note: required by a bound in `assert_test_result`

0 commit comments

Comments
 (0)