Emit an error when RTN is used in an incorrect place

We miss one place: associated type bindings aka. `impl Trait<Type(..): Send>`, but we also miss it for Fn-style parenthesizes error so I left it out for now.
This commit is contained in:
Chayim Refael Friedman 2025-03-08 20:56:54 +02:00
parent eaa0a39831
commit 5076ef7d9b
4 changed files with 80 additions and 2 deletions

View file

@ -0,0 +1,52 @@
use ide_db::Severity;
use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};
// Diagnostic: bad-rtn
//
// This diagnostic is shown when a RTN (Return Type Notation, `Type::method(..): Send`) is written in an improper place.
pub(crate) fn bad_rtn(ctx: &DiagnosticsContext<'_>, d: &hir::BadRtn) -> Diagnostic {
Diagnostic::new_with_syntax_node_ptr(
ctx,
DiagnosticCode::Ra("bad-rtn", Severity::Error),
"return type notation not allowed in this position yet",
d.rtn.map(Into::into),
)
}
#[cfg(test)]
mod tests {
use crate::tests::check_diagnostics;
#[test]
fn fn_traits_also_emit() {
check_diagnostics(
r#"
//- minicore: fn
fn foo<
A: Fn(..),
// ^^^^ error: return type notation not allowed in this position yet
>() {}
"#,
);
}
#[test]
fn bad_rtn() {
check_diagnostics(
r#"
mod module {
pub struct Type;
}
trait Trait {}
fn foo()
where
module(..)::Type: Trait
// ^^^^ error: return type notation not allowed in this position yet
{
}
"#,
);
}
}

View file

@ -25,6 +25,7 @@
mod handlers {
pub(crate) mod await_outside_of_async;
pub(crate) mod bad_rtn;
pub(crate) mod break_outside_of_loop;
pub(crate) mod expected_function;
pub(crate) mod generic_args_prohibited;
@ -493,6 +494,7 @@ pub fn semantic_diagnostics(
AnyDiagnostic::ParenthesizedGenericArgsWithoutFnTrait(d) => {
handlers::parenthesized_generic_args_without_fn_trait::parenthesized_generic_args_without_fn_trait(&ctx, &d)
}
AnyDiagnostic::BadRtn(d) => handlers::bad_rtn::bad_rtn(&ctx, &d),
};
res.push(d)
}