Merge pull request #19127 from ChayimFriedman2/different-generic-args

feat: Refactor path lowering and serve a new path diagnostic
This commit is contained in:
Lukas Wirth 2025-02-17 08:30:10 +00:00 committed by GitHub
commit 09db657439
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 1222 additions and 1144 deletions

View file

@ -0,0 +1,59 @@
use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};
// Diagnostic: parenthesized-generic-args-without-fn-trait
//
// This diagnostic is shown when a `Fn`-trait-style generic parameters (`Trait(A, B) -> C`)
// was used on non-`Fn` trait/type.
pub(crate) fn parenthesized_generic_args_without_fn_trait(
ctx: &DiagnosticsContext<'_>,
d: &hir::ParenthesizedGenericArgsWithoutFnTrait,
) -> Diagnostic {
Diagnostic::new_with_syntax_node_ptr(
ctx,
DiagnosticCode::RustcHardError("E0214"),
"parenthesized type parameters may only be used with a `Fn` trait",
d.args.map(Into::into),
)
}
#[cfg(test)]
mod tests {
use crate::tests::check_diagnostics;
#[test]
fn fn_traits_work() {
check_diagnostics(
r#"
//- minicore: async_fn, fn
fn foo<
A: Fn(),
B: FnMut() -> i32,
C: FnOnce(&str, bool),
D: AsyncFn::(u32) -> u32,
E: AsyncFnMut(),
F: AsyncFnOnce() -> bool,
>() {}
"#,
);
}
#[test]
fn non_fn_trait() {
check_diagnostics(
r#"
struct Struct<T>(T);
enum Enum<T> { EnumVariant(T) }
type TypeAlias<T> = bool;
type Foo = TypeAlias() -> bool;
// ^^ error: parenthesized type parameters may only be used with a `Fn` trait
fn foo(_a: Struct(i32)) {
// ^^^^^ error: parenthesized type parameters may only be used with a `Fn` trait
let _ = <Enum::(u32)>::EnumVariant(0);
// ^^^^^^^ error: parenthesized type parameters may only be used with a `Fn` trait
}
"#,
);
}
}

View file

@ -43,6 +43,7 @@ mod handlers {
pub(crate) mod mutability_errors;
pub(crate) mod no_such_field;
pub(crate) mod non_exhaustive_let;
pub(crate) mod parenthesized_generic_args_without_fn_trait;
pub(crate) mod private_assoc_item;
pub(crate) mod private_field;
pub(crate) mod remove_trailing_return;
@ -466,7 +467,12 @@ pub fn semantic_diagnostics(
Some(it) => it,
None => continue,
},
AnyDiagnostic::GenericArgsProhibited(d) => handlers::generic_args_prohibited::generic_args_prohibited(&ctx, &d)
AnyDiagnostic::GenericArgsProhibited(d) => {
handlers::generic_args_prohibited::generic_args_prohibited(&ctx, &d)
}
AnyDiagnostic::ParenthesizedGenericArgsWithoutFnTrait(d) => {
handlers::parenthesized_generic_args_without_fn_trait::parenthesized_generic_args_without_fn_trait(&ctx, &d)
}
};
res.push(d)
}