Skip to content

Commit 5076ef7

Browse files
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.
1 parent eaa0a39 commit 5076ef7

File tree

4 files changed

+80
-2
lines changed

4 files changed

+80
-2
lines changed

crates/hir-ty/src/lower/path.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -607,8 +607,14 @@ impl<'a, 'b> PathLoweringContext<'a, 'b> {
607607
) -> Substitution {
608608
let prohibit_parens = match def {
609609
GenericDefId::TraitId(trait_) => {
610-
let trait_data = self.ctx.db.trait_data(trait_);
611-
!trait_data.flags.contains(TraitFlags::RUSTC_PAREN_SUGAR)
610+
// RTN is prohibited anyways if we got here.
611+
let is_rtn =
612+
self.current_or_prev_segment.args_and_bindings.is_some_and(|generics| {
613+
generics.parenthesized == GenericArgsParentheses::ReturnTypeNotation
614+
});
615+
let is_fn_trait =
616+
!self.ctx.db.trait_data(trait_).flags.contains(TraitFlags::RUSTC_PAREN_SUGAR);
617+
is_rtn || is_fn_trait
612618
}
613619
_ => true,
614620
};

crates/hir/src/diagnostics.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ diagnostics![
113113
UnusedVariable,
114114
GenericArgsProhibited,
115115
ParenthesizedGenericArgsWithoutFnTrait,
116+
BadRtn,
116117
];
117118

118119
#[derive(Debug)]
@@ -420,6 +421,11 @@ pub struct ParenthesizedGenericArgsWithoutFnTrait {
420421
pub args: InFile<AstPtr<ast::ParenthesizedArgList>>,
421422
}
422423

424+
#[derive(Debug)]
425+
pub struct BadRtn {
426+
pub rtn: InFile<AstPtr<ast::ReturnTypeSyntax>>,
427+
}
428+
423429
impl AnyDiagnostic {
424430
pub(crate) fn body_validation_diagnostic(
425431
db: &dyn HirDatabase,
@@ -712,6 +718,12 @@ impl AnyDiagnostic {
712718
Some(match *diag {
713719
PathLoweringDiagnostic::GenericArgsProhibited { segment, reason } => {
714720
let segment = hir_segment_to_ast_segment(&path.value, segment)?;
721+
722+
if let Some(rtn) = segment.return_type_syntax() {
723+
// RTN errors are emitted as `GenericArgsProhibited` or `ParenthesizedGenericArgsWithoutFnTrait`.
724+
return Some(BadRtn { rtn: path.with_value(AstPtr::new(&rtn)) }.into());
725+
}
726+
715727
let args = if let Some(generics) = segment.generic_arg_list() {
716728
AstPtr::new(&generics).wrap_left()
717729
} else {
@@ -722,6 +734,12 @@ impl AnyDiagnostic {
722734
}
723735
PathLoweringDiagnostic::ParenthesizedGenericArgsWithoutFnTrait { segment } => {
724736
let segment = hir_segment_to_ast_segment(&path.value, segment)?;
737+
738+
if let Some(rtn) = segment.return_type_syntax() {
739+
// RTN errors are emitted as `GenericArgsProhibited` or `ParenthesizedGenericArgsWithoutFnTrait`.
740+
return Some(BadRtn { rtn: path.with_value(AstPtr::new(&rtn)) }.into());
741+
}
742+
725743
let args = AstPtr::new(&segment.parenthesized_arg_list()?);
726744
let args = path.with_value(args);
727745
ParenthesizedGenericArgsWithoutFnTrait { args }.into()
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use ide_db::Severity;
2+
3+
use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};
4+
5+
// Diagnostic: bad-rtn
6+
//
7+
// This diagnostic is shown when a RTN (Return Type Notation, `Type::method(..): Send`) is written in an improper place.
8+
pub(crate) fn bad_rtn(ctx: &DiagnosticsContext<'_>, d: &hir::BadRtn) -> Diagnostic {
9+
Diagnostic::new_with_syntax_node_ptr(
10+
ctx,
11+
DiagnosticCode::Ra("bad-rtn", Severity::Error),
12+
"return type notation not allowed in this position yet",
13+
d.rtn.map(Into::into),
14+
)
15+
}
16+
17+
#[cfg(test)]
18+
mod tests {
19+
use crate::tests::check_diagnostics;
20+
21+
#[test]
22+
fn fn_traits_also_emit() {
23+
check_diagnostics(
24+
r#"
25+
//- minicore: fn
26+
fn foo<
27+
A: Fn(..),
28+
// ^^^^ error: return type notation not allowed in this position yet
29+
>() {}
30+
"#,
31+
);
32+
}
33+
34+
#[test]
35+
fn bad_rtn() {
36+
check_diagnostics(
37+
r#"
38+
mod module {
39+
pub struct Type;
40+
}
41+
trait Trait {}
42+
43+
fn foo()
44+
where
45+
module(..)::Type: Trait
46+
// ^^^^ error: return type notation not allowed in this position yet
47+
{
48+
}
49+
"#,
50+
);
51+
}
52+
}

crates/ide-diagnostics/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
2626
mod handlers {
2727
pub(crate) mod await_outside_of_async;
28+
pub(crate) mod bad_rtn;
2829
pub(crate) mod break_outside_of_loop;
2930
pub(crate) mod expected_function;
3031
pub(crate) mod generic_args_prohibited;
@@ -493,6 +494,7 @@ pub fn semantic_diagnostics(
493494
AnyDiagnostic::ParenthesizedGenericArgsWithoutFnTrait(d) => {
494495
handlers::parenthesized_generic_args_without_fn_trait::parenthesized_generic_args_without_fn_trait(&ctx, &d)
495496
}
497+
AnyDiagnostic::BadRtn(d) => handlers::bad_rtn::bad_rtn(&ctx, &d),
496498
};
497499
res.push(d)
498500
}

0 commit comments

Comments
 (0)