Skip to content

feat: Add two new diagnostics: one for mismatch in generic arguments count, and another for mismatch in their kind #19479

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion crates/hir-def/src/expr_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ use crate::{
};

pub use self::body::{Body, BodySourceMap};
pub use self::lower::hir_segment_to_ast_segment;
pub use self::lower::{
hir_assoc_type_binding_to_ast, hir_generic_arg_to_ast, hir_segment_to_ast_segment,
};

/// A wrapper around [`span::SyntaxContextId`] that is intended only for comparisons.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
Expand Down
32 changes: 32 additions & 0 deletions crates/hir-def/src/expr_store/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,7 @@ impl ExprCollector<'_> {
node: ast::GenericArgList,
impl_trait_lower_fn: &mut impl FnMut(ThinVec<TypeBound>) -> TypeRef,
) -> Option<GenericArgs> {
// This needs to be kept in sync with `hir_generic_arg_to_ast()`.
let mut args = Vec::new();
let mut bindings = Vec::new();
for generic_arg in node.generic_args() {
Expand All @@ -798,6 +799,7 @@ impl ExprCollector<'_> {
args.push(GenericArg::Type(type_ref));
}
ast::GenericArg::AssocTypeArg(assoc_type_arg) => {
// This needs to be kept in sync with `hir_assoc_type_binding_to_ast()`.
if assoc_type_arg.param_list().is_some() {
// We currently ignore associated return type bounds.
continue;
Expand Down Expand Up @@ -3227,3 +3229,33 @@ enum ArgumentType {
Format(FormatTrait),
Usize,
}

/// This function find the AST fragment that corresponds to an `AssociatedTypeBinding` in the HIR.
pub fn hir_assoc_type_binding_to_ast(
segment_args: &ast::GenericArgList,
binding_idx: u32,
) -> Option<ast::AssocTypeArg> {
segment_args
.generic_args()
.filter_map(|arg| match arg {
ast::GenericArg::AssocTypeArg(it) => Some(it),
_ => None,
})
.filter(|binding| binding.param_list().is_none() && binding.name_ref().is_some())
.nth(binding_idx as usize)
}

/// This function find the AST generic argument from the one in the HIR. Does not support the `Self` argument.
pub fn hir_generic_arg_to_ast(
args: &ast::GenericArgList,
arg_idx: u32,
has_self_arg: bool,
) -> Option<ast::GenericArg> {
args.generic_args()
.filter(|arg| match arg {
ast::GenericArg::AssocTypeArg(_) => false,
ast::GenericArg::LifetimeArg(arg) => arg.lifetime().is_some(),
ast::GenericArg::ConstArg(_) | ast::GenericArg::TypeArg(_) => true,
})
.nth(arg_idx as usize - has_self_arg as usize)
}
4 changes: 1 addition & 3 deletions crates/hir-def/src/expr_store/lower/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,8 @@ pub(super) fn lower_path(
args: iter::once(self_type)
.chain(it.args.iter().cloned())
.collect(),

has_self_type: true,
bindings: it.bindings.clone(),
parenthesized: it.parenthesized,
..it
},
None => GenericArgs {
args: Box::new([self_type]),
Expand Down
1 change: 1 addition & 0 deletions crates/hir-def/src/hir/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ impl GenericParamData {

impl_from!(TypeParamData, ConstParamData, LifetimeParamData for GenericParamData);

#[derive(Debug, Clone, Copy)]
pub enum GenericParamDataRef<'a> {
TypeParamData(&'a TypeParamData),
ConstParamData(&'a ConstParamData),
Expand Down
39 changes: 19 additions & 20 deletions crates/hir-ty/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,29 +304,28 @@ impl TyBuilder<hir_def::AdtId> {
// Note that we're building ADT, so we never have parent generic parameters.
let defaults = db.generic_defaults(self.data.into());

for default_ty in &defaults[self.vec.len()..] {
// NOTE(skip_binders): we only check if the arg type is error type.
if let Some(x) = default_ty.skip_binders().ty(Interner) {
if x.is_unknown() {
self.vec.push(fallback().cast(Interner));
continue;
if let Some(defaults) = defaults.get(self.vec.len()..) {
for default_ty in defaults {
// NOTE(skip_binders): we only check if the arg type is error type.
if let Some(x) = default_ty.skip_binders().ty(Interner) {
if x.is_unknown() {
self.vec.push(fallback().cast(Interner));
continue;
}
}
// Each default can only depend on the previous parameters.
self.vec.push(default_ty.clone().substitute(Interner, &*self.vec).cast(Interner));
}
// Each default can only depend on the previous parameters.
let subst_so_far = Substitution::from_iter(
Interner,
self.vec
.iter()
.cloned()
.chain(self.param_kinds[self.vec.len()..].iter().map(|it| match it {
ParamKind::Type => TyKind::Error.intern(Interner).cast(Interner),
ParamKind::Lifetime => error_lifetime().cast(Interner),
ParamKind::Const(ty) => unknown_const_as_generic(ty.clone()),
}))
.take(self.param_kinds.len()),
);
self.vec.push(default_ty.clone().substitute(Interner, &subst_so_far).cast(Interner));
}

// The defaults may be missing if no param has default, so fill that.
let filler = self.param_kinds[self.vec.len()..].iter().map(|x| match x {
ParamKind::Type => fallback().cast(Interner),
ParamKind::Const(ty) => unknown_const_as_generic(ty.clone()),
ParamKind::Lifetime => error_lifetime().cast(Interner),
});
self.vec.extend(filler.casted(Interner));

self
}

Expand Down
3 changes: 3 additions & 0 deletions crates/hir-ty/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ pub trait HirDatabase: DefDatabase + std::fmt::Debug {
def: GenericDefId,
) -> (GenericDefaults, Diagnostics);

/// This returns an empty list if no parameter has default.
///
/// The binders of the returned defaults are only up to (not including) this parameter.
#[salsa::invoke_actual(crate::lower::generic_defaults_query)]
fn generic_defaults(&self, def: GenericDefId) -> GenericDefaults;

Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1638,7 +1638,7 @@ fn generic_args_sans_defaults<'ga>(
Some(default_parameter) => {
// !is_err(default_parameter.skip_binders())
// &&
arg != &default_parameter.clone().substitute(Interner, &parameters)
arg != &default_parameter.clone().substitute(Interner, &parameters[..i])
}
}
};
Expand Down
16 changes: 11 additions & 5 deletions crates/hir-ty/src/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use hir_def::{
},
};
use itertools::chain;
use stdx::TupleExt;
use triomphe::Arc;

use crate::{Interner, Substitution, db::HirDatabase, lt_to_placeholder_idx, to_placeholder_idx};
Expand Down Expand Up @@ -76,10 +75,13 @@ impl Generics {
self.iter_parent().map(|(id, _)| id)
}

pub(crate) fn iter_self_type_or_consts_id(
pub(crate) fn iter_self_type_or_consts(
&self,
) -> impl DoubleEndedIterator<Item = GenericParamId> + '_ {
self.params.iter_type_or_consts().map(from_toc_id(self)).map(TupleExt::head)
) -> impl DoubleEndedIterator<Item = (LocalTypeOrConstParamId, &TypeOrConstParamData)> + '_
{
let mut toc = self.params.iter_type_or_consts();
let trait_self_param = self.has_trait_self_param.then(|| toc.next()).flatten();
chain!(trait_self_param, toc)
}

/// Iterate over the params followed by the parent params.
Expand Down Expand Up @@ -129,6 +131,10 @@ impl Generics {
self.params.len()
}

pub(crate) fn len_lifetimes_self(&self) -> usize {
self.params.len_lifetimes()
}

/// (parent total, self param, type params, const params, impl trait list, lifetimes)
pub(crate) fn provenance_split(&self) -> (usize, bool, usize, usize, usize, usize) {
let mut self_param = false;
Expand All @@ -144,7 +150,7 @@ impl Generics {
TypeOrConstParamData::ConstParamData(_) => const_params += 1,
});

let lifetime_params = self.params.iter_lt().count();
let lifetime_params = self.params.len_lifetimes();

let parent_len = self.parent_generics().map_or(0, Generics::len);
(parent_len, self_param, type_params, const_params, impl_trait_params, lifetime_params)
Expand Down
33 changes: 25 additions & 8 deletions crates/hir-ty/src/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ use chalk_ir::{
};
use either::Either;
use hir_def::{
AdtId, AssocItemId, DefWithBodyId, FieldId, FunctionId, GenericDefId, ImplId, ItemContainerId,
Lookup, TraitId, TupleFieldId, TupleId, TypeAliasId, VariantId,
AdtId, AssocItemId, DefWithBodyId, FieldId, FunctionId, GenericDefId, GenericParamId, ImplId,
ItemContainerId, Lookup, TraitId, TupleFieldId, TupleId, TypeAliasId, VariantId,
builtin_type::{BuiltinInt, BuiltinType, BuiltinUint},
expr_store::{Body, ExpressionStore, HygieneId, path::Path},
hir::{BindingAnnotation, BindingId, ExprId, ExprOrPatId, LabelId, PatId},
Expand All @@ -55,8 +55,9 @@ use triomphe::Arc;

use crate::{
AliasEq, AliasTy, Binders, ClosureId, Const, DomainGoal, GenericArg, Goal, ImplTraitId,
ImplTraitIdx, InEnvironment, Interner, Lifetime, OpaqueTyId, ParamLoweringMode,
PathLoweringDiagnostic, ProjectionTy, Substitution, TraitEnvironment, Ty, TyBuilder, TyExt,
ImplTraitIdx, InEnvironment, IncorrectGenericsLenKind, Interner, Lifetime, OpaqueTyId,
ParamLoweringMode, PathLoweringDiagnostic, ProjectionTy, Substitution, TraitEnvironment, Ty,
TyBuilder, TyExt,
db::HirDatabase,
fold_tys,
generics::Generics,
Expand All @@ -66,7 +67,7 @@ use crate::{
expr::ExprIsRead,
unify::InferenceTable,
},
lower::{ImplTraitLoweringMode, diagnostics::TyLoweringDiagnostic},
lower::{GenericArgsPosition, ImplTraitLoweringMode, diagnostics::TyLoweringDiagnostic},
mir::MirSpan,
to_assoc_type_id,
traits::FnTrait,
Expand Down Expand Up @@ -275,6 +276,20 @@ pub enum InferenceDiagnostic {
node: ExprOrPatId,
diag: PathLoweringDiagnostic,
},
MethodCallIncorrectGenericsLen {
expr: ExprId,
provided_count: u32,
expected_count: u32,
kind: IncorrectGenericsLenKind,
def: GenericDefId,
},
MethodCallIncorrectGenericsOrder {
expr: ExprId,
param_id: GenericParamId,
arg_idx: u32,
/// Whether the `GenericArgs` contains a `Self` arg.
has_self_arg: bool,
},
}

/// A mismatch between an expected and an inferred type.
Expand Down Expand Up @@ -909,6 +924,7 @@ impl<'a> InferenceContext<'a> {
let mut param_tys =
self.with_ty_lowering(&data.store, InferenceTyDiagnosticSource::Signature, |ctx| {
ctx.type_param_mode(ParamLoweringMode::Placeholder);
ctx.in_fn_signature = true;
data.params.iter().map(|&type_ref| ctx.lower_ty(type_ref)).collect::<Vec<_>>()
});

Expand Down Expand Up @@ -953,8 +969,9 @@ impl<'a> InferenceContext<'a> {
InferenceTyDiagnosticSource::Signature,
|ctx| {
ctx.type_param_mode(ParamLoweringMode::Placeholder)
.impl_trait_mode(ImplTraitLoweringMode::Opaque)
.lower_ty(return_ty)
.impl_trait_mode(ImplTraitLoweringMode::Opaque);
ctx.in_fn_signature = true;
ctx.lower_ty(return_ty)
},
);
let return_ty = self.insert_type_vars(return_ty);
Expand Down Expand Up @@ -1513,7 +1530,7 @@ impl<'a> InferenceContext<'a> {
InferenceTyDiagnosticSource::Body,
self.generic_def,
);
let mut path_ctx = ctx.at_path(path, node);
let mut path_ctx = ctx.at_path(path, node, GenericArgsPosition::Value);
let (resolution, unresolved) = if value_ns {
let Some(res) = path_ctx.resolve_path_in_value_ns(HygieneId::ROOT) else {
return (self.err_ty(), None);
Expand Down
7 changes: 5 additions & 2 deletions crates/hir-ty/src/infer/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use hir_def::expr_store::path::Path;
use hir_def::{hir::ExprOrPatId, resolver::Resolver};
use la_arena::{Idx, RawIdx};

use crate::lower::GenericArgsPosition;
use crate::{
InferenceDiagnostic, InferenceTyDiagnosticSource, TyLoweringContext, TyLoweringDiagnostic,
db::HirDatabase,
Expand Down Expand Up @@ -74,6 +75,7 @@ impl<'a> InferenceTyLoweringContext<'a> {
&'b mut self,
path: &'b Path,
node: ExprOrPatId,
position: GenericArgsPosition,
) -> PathLoweringContext<'b, 'a> {
let on_diagnostic = PathDiagnosticCallback {
data: Either::Right(PathDiagnosticCallbackData { diagnostics: self.diagnostics, node }),
Expand All @@ -83,13 +85,14 @@ impl<'a> InferenceTyLoweringContext<'a> {
.push(InferenceDiagnostic::PathDiagnostic { node: data.node, diag });
},
};
PathLoweringContext::new(&mut self.ctx, on_diagnostic, path)
PathLoweringContext::new(&mut self.ctx, on_diagnostic, path, position)
}

#[inline]
pub(super) fn at_path_forget_diagnostics<'b>(
&'b mut self,
path: &'b Path,
position: GenericArgsPosition,
) -> PathLoweringContext<'b, 'a> {
let on_diagnostic = PathDiagnosticCallback {
data: Either::Right(PathDiagnosticCallbackData {
Expand All @@ -98,7 +101,7 @@ impl<'a> InferenceTyLoweringContext<'a> {
}),
callback: |_data, _, _diag| {},
};
PathLoweringContext::new(&mut self.ctx, on_diagnostic, path)
PathLoweringContext::new(&mut self.ctx, on_diagnostic, path, position)
}

#[inline]
Expand Down
Loading