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

@ -607,8 +607,14 @@ impl<'a, 'b> PathLoweringContext<'a, 'b> {
) -> Substitution {
let prohibit_parens = match def {
GenericDefId::TraitId(trait_) => {
let trait_data = self.ctx.db.trait_data(trait_);
!trait_data.flags.contains(TraitFlags::RUSTC_PAREN_SUGAR)
// RTN is prohibited anyways if we got here.
let is_rtn =
self.current_or_prev_segment.args_and_bindings.is_some_and(|generics| {
generics.parenthesized == GenericArgsParentheses::ReturnTypeNotation
});
let is_fn_trait =
!self.ctx.db.trait_data(trait_).flags.contains(TraitFlags::RUSTC_PAREN_SUGAR);
is_rtn || is_fn_trait
}
_ => true,
};

View file

@ -113,6 +113,7 @@ diagnostics![
UnusedVariable,
GenericArgsProhibited,
ParenthesizedGenericArgsWithoutFnTrait,
BadRtn,
];
#[derive(Debug)]
@ -420,6 +421,11 @@ pub struct ParenthesizedGenericArgsWithoutFnTrait {
pub args: InFile<AstPtr<ast::ParenthesizedArgList>>,
}
#[derive(Debug)]
pub struct BadRtn {
pub rtn: InFile<AstPtr<ast::ReturnTypeSyntax>>,
}
impl AnyDiagnostic {
pub(crate) fn body_validation_diagnostic(
db: &dyn HirDatabase,
@ -712,6 +718,12 @@ impl AnyDiagnostic {
Some(match *diag {
PathLoweringDiagnostic::GenericArgsProhibited { segment, reason } => {
let segment = hir_segment_to_ast_segment(&path.value, segment)?;
if let Some(rtn) = segment.return_type_syntax() {
// RTN errors are emitted as `GenericArgsProhibited` or `ParenthesizedGenericArgsWithoutFnTrait`.
return Some(BadRtn { rtn: path.with_value(AstPtr::new(&rtn)) }.into());
}
let args = if let Some(generics) = segment.generic_arg_list() {
AstPtr::new(&generics).wrap_left()
} else {
@ -722,6 +734,12 @@ impl AnyDiagnostic {
}
PathLoweringDiagnostic::ParenthesizedGenericArgsWithoutFnTrait { segment } => {
let segment = hir_segment_to_ast_segment(&path.value, segment)?;
if let Some(rtn) = segment.return_type_syntax() {
// RTN errors are emitted as `GenericArgsProhibited` or `ParenthesizedGenericArgsWithoutFnTrait`.
return Some(BadRtn { rtn: path.with_value(AstPtr::new(&rtn)) }.into());
}
let args = AstPtr::new(&segment.parenthesized_arg_list()?);
let args = path.with_value(args);
ParenthesizedGenericArgsWithoutFnTrait { args }.into()

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)
}