Skip to content
Merged
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
9 changes: 2 additions & 7 deletions crates/fmt/src/ast/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,15 +474,10 @@ impl ToDoc for ast::FuncParam {
doc = doc.append(alloc.text("mut "));
}

let label = self.label();
let name = self.name();

if let (Some(label), Some(name_ref)) = (&label, &name)
&& label.syntax().text_range() != name_ref.syntax().text_range()
{
doc =
doc.append(alloc.text(ctx.snippet(label.syntax().text_range()).trim().to_string()));
doc = doc.append(alloc.text(" "));
if self.is_label_suppressed() {
doc = doc.append(alloc.text("_ "));
}

if let Some(name) = name {
Expand Down
32 changes: 0 additions & 32 deletions crates/hir/src/analysis/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1285,38 +1285,6 @@ impl DiagnosticVoucher for TyLowerDiag<'_> {
}
}

Self::DuplicateArgLabel(func, idxs) => {
let views: Vec<_> = func.params(db).collect();
let name = views[idxs[0] as usize]
.label_eagerly(db)
.expect("param label")
.data(db);

let spans = idxs.iter().map(|i| {
let i = *i as usize;
let s = func.span().params().clone().param(i);
if views[i].label(db).is_some() {
s.label().resolve(db)
} else {
s.name().resolve(db)
}
});

let message = if let Some(name) = func.name(db).to_opt() {
format!("duplicate argument label in function `{}`", name.data(db))
} else {
"duplicate argument label in function definition".into()
};

CompleteDiagnostic {
severity: Severity::Error,
message,
sub_diagnostics: duplicate_name_subdiags(name, spans),
notes: vec![],
error_code,
}
}

Self::DuplicateFieldName(parent, idxs) => {
let name = parent
.fields(db)
Expand Down
2 changes: 0 additions & 2 deletions crates/hir/src/analysis/ty/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ pub enum TyLowerDiag<'db> {
},

DuplicateArgName(Func<'db>, SmallVec<[u16; 4]>),
DuplicateArgLabel(Func<'db>, SmallVec<[u16; 4]>),
DuplicateFieldName(FieldParent<'db>, SmallVec<[u16; 4]>),
DuplicateVariantName(Enum<'db>, SmallVec<[u16; 4]>),
DuplicateGenericParamName(GenericParamOwner<'db>, SmallVec<[u16; 4]>),
Expand Down Expand Up @@ -156,7 +155,6 @@ impl TyLowerDiag<'_> {
Self::DuplicateFieldName(..) => 17,
Self::DuplicateVariantName(..) => 18,
Self::DuplicateGenericParamName(..) => 19,
Self::DuplicateArgLabel(..) => 20,
Self::NonTrailingDefaultGenericParam(_) => 21,
Self::GenericDefaultForwardRef { .. } => 22,
}
Expand Down
6 changes: 5 additions & 1 deletion crates/hir/src/core/hir_def/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,11 @@ impl<'db> Func<'db> {

pub fn param_label_or_name(self, db: &'db dyn HirDb, idx: usize) -> Option<FuncParamName<'db>> {
let param = self.params_list(db).to_opt()?.data(db).get(idx)?;
param.label.or(param.name.to_opt())
if param.is_label_suppressed {
Some(FuncParamName::Underscore)
} else {
param.name.to_opt()
}
}

/// View as a callable definition (if named).
Expand Down
20 changes: 8 additions & 12 deletions crates/hir/src/core/hir_def/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ pub struct AssocTypeGenericArg<'db> {
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct FuncParam<'db> {
pub is_mut: bool,
pub label: Option<FuncParamName<'db>>,
pub is_label_suppressed: bool,
pub name: Partial<FuncParamName<'db>>,
pub ty: Partial<TypeId<'db>>,

Expand All @@ -194,17 +194,13 @@ pub struct FuncParam<'db> {

impl<'db> FuncParam<'db> {
pub fn label_eagerly(&self) -> Option<IdentId<'db>> {
match self.label {
Some(FuncParamName::Ident(ident)) => return Some(ident),
Some(FuncParamName::Underscore) => return None,
_ => {}
}

if let FuncParamName::Ident(ident) = self.name.to_opt()? {
Some(ident)
} else {
None
}
(!self.is_label_suppressed)
.then(|| self.name.to_opt())
.flatten()
.and_then(|name| match name {
FuncParamName::Ident(ident) => Some(ident),
FuncParamName::Underscore => None,
})
}

pub fn name(&self) -> Option<IdentId<'db>> {
Expand Down
19 changes: 5 additions & 14 deletions crates/hir/src/core/hir_def/scope_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use salsa::Update;

use super::{
AssocConstDecl, AssocTyDecl, AttrListId, Body, Const, Contract, Enum, EnumVariant, ExprId,
FieldDef, FieldParent, Func, FuncParam, FuncParamName, GenericParam, IdentId, Impl, ImplTrait,
ItemKind, Mod, Struct, TopLevelMod, Trait, TypeAlias, Use, VariantDef, VariantKind, Visibility,
FieldDef, FieldParent, Func, FuncParam, GenericParam, IdentId, Impl, ImplTrait, ItemKind, Mod,
Struct, TopLevelMod, Trait, TypeAlias, Use, VariantDef, VariantKind, Visibility,
scope_graph_viz::ScopeGraphFormatter,
};
use crate::{
Expand Down Expand Up @@ -339,11 +339,7 @@ impl<'db> ScopeId<'db> {

ScopeId::FuncParam(..) => {
let param: &FuncParam = self.resolve_to(db).unwrap();
if let Some(FuncParamName::Ident(ident)) = param.label {
Some(ident)
} else {
param.name()
}
param.name()
}

ScopeId::GenericParam(..) => {
Expand All @@ -358,7 +354,7 @@ impl<'db> ScopeId<'db> {
}
}

pub fn name_span(self, db: &'db dyn HirDb) -> Option<DynLazySpan<'db>> {
pub fn name_span(self, _db: &'db dyn HirDb) -> Option<DynLazySpan<'db>> {
match self {
ScopeId::Item(item) => item.name_span(),

Expand All @@ -368,13 +364,8 @@ impl<'db> ScopeId<'db> {

ScopeId::FuncParam(parent, idx) => {
let func: Func = parent.try_into().unwrap();
let param = &func.params_list(db).to_opt()?.data(db)[idx as usize];
let param_span = func.span().params().param(idx as usize);
if let Some(FuncParamName::Ident(_)) = param.label {
Some(param_span.label().into())
} else {
Some(param_span.name().into())
}
Some(param_span.name().into())
}

ScopeId::GenericParam(parent, idx) => {
Expand Down
4 changes: 2 additions & 2 deletions crates/hir/src/core/lower/hir_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ where
let db = self.db();
FuncParam {
is_mut: false,
label: None,
is_label_suppressed: false,
name: Partial::Present(FuncParamName::Ident(IdentId::make_self(db))),
ty: Partial::Present(self.self_ty()),
self_ty_fallback: true,
Expand All @@ -193,7 +193,7 @@ where
) -> FuncParam<'db> {
FuncParam {
is_mut: true,
label: Some(FuncParamName::Underscore),
is_label_suppressed: true,
name: Partial::Present(FuncParamName::Ident(name)),
ty: Partial::Present(ty),
self_ty_fallback: false,
Expand Down
11 changes: 2 additions & 9 deletions crates/hir/src/core/lower/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl<'db> GenericParam<'db> {
impl<'db> FuncParam<'db> {
fn lower_ast(ctxt: &mut FileLowerCtxt<'db>, ast: ast::FuncParam) -> Self {
let is_mut = ast.mut_token().is_some();
let label = ast.label().map(|ast| FuncParamName::lower_label(ctxt, ast));
let is_label_suppressed = ast.is_label_suppressed();
let name = ast.name().map(|ast| FuncParamName::lower_ast(ctxt, ast));

let self_ty_fallback =
Expand All @@ -169,7 +169,7 @@ impl<'db> FuncParam<'db> {

Self {
is_mut,
label,
is_label_suppressed,
name: name.into(),
ty,
self_ty_fallback,
Expand Down Expand Up @@ -239,11 +239,4 @@ impl<'db> FuncParamName<'db> {
ast::FuncParamName::Underscore(_) => FuncParamName::Underscore,
}
}

fn lower_label(ctxt: &mut FileLowerCtxt<'db>, ast: ast::FuncParamLabel) -> FuncParamName<'db> {
match ast {
ast::FuncParamLabel::Ident(name) => Self::Ident(IdentId::lower_token(ctxt, name)),
ast::FuncParamLabel::Underscore(_) => Self::Underscore,
}
}
}
9 changes: 2 additions & 7 deletions crates/hir/src/core/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,13 +367,8 @@ impl<'db> FuncParam<'db> {
result.push_str("mut ");
}

// Handle label if different from name
if let Some(label) = &self.label {
// Only print label separately if different from name
if *label != name {
result.push_str(&label.pretty_print(db));
result.push(' ');
}
if self.is_label_suppressed {
result.push_str("_ ");
}

// Name
Expand Down
14 changes: 11 additions & 3 deletions crates/hir/src/core/semantic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,17 @@ impl<'db> FuncParamView<'db> {

pub fn label(self, db: &'db dyn HirDb) -> Option<IdentId<'db>> {
let list = self.func.params_list(db).to_opt()?;
match list.data(db).get(self.idx)?.label {
Some(FuncParamName::Ident(id)) => Some(id),
_ => None,
let param = list.data(db).get(self.idx)?;
(!param.is_label_suppressed && !param.is_self_param(db))
.then(|| param.name())
.flatten()
}

pub fn is_label_suppressed(self, db: &'db dyn HirDb) -> bool {
let list = self.func.params_list(db).to_opt();
match list.and_then(|l| l.data(db).get(self.idx)) {
Some(p) => p.is_label_suppressed,
None => false,
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/hir/src/core/span/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ mod tests {
let mut db = TestDb::default();

let text = r#"
fn my_func<T: Debug, U, const LEN: usize>(x: u32, label y: foo::Bar<2>) -> FooResult
fn my_func<T: Debug, U, const LEN: usize>(x: u32, _ y: foo::Bar<2>) -> FooResult
where U: Add
"#;

Expand Down Expand Up @@ -713,7 +713,7 @@ mod tests {

assert_eq!("x", db.text_at(top_mod, &param_1.clone().name()));
assert_eq!("u32", db.text_at(top_mod, &param_1.ty()));
assert_eq!("label", db.text_at(top_mod, &param_2.clone().label()));
assert_eq!("y", db.text_at(top_mod, &param_2.clone().name()));
assert_eq!("foo::Bar<2>", db.text_at(top_mod, &param_2.ty()));

assert_eq!("FooResult", db.text_at(top_mod, &fn_.span().ret_ty()));
Expand Down
1 change: 0 additions & 1 deletion crates/hir/src/core/span/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ define_lazy_span_node!(
(mut_kw, mut_token),
}
@node {
(label, label, LazySpanAtom),
(name, name, LazySpanAtom),
(ty, ty, LazyTySpan),
}
Expand Down
4 changes: 0 additions & 4 deletions crates/hir/src/core/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1754,10 +1754,6 @@ pub fn walk_func_param<'db, V>(
) where
V: Visitor<'db> + ?Sized,
{
if let Some(FuncParamName::Ident(ident)) = param.label {
ctxt.with_new_ctxt(|span| span.label(), |ctxt| visitor.visit_ident(ctxt, ident));
}

if let Some(FuncParamName::Ident(ident)) = param.name.to_opt() {
ctxt.with_new_ctxt(|span| span.name(), |ctxt| visitor.visit_ident(ctxt, ident));
}
Expand Down
21 changes: 4 additions & 17 deletions crates/hir/src/diagnosable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,24 +266,11 @@ impl<'db> WherePredicateBoundView<'db> {
impl<'db> Func<'db> {
/// Diagnostics related to parameters (duplicate names/labels).
pub fn diags_parameters(self, db: &'db dyn HirAnalysisDb) -> Vec<TyDiagCollection<'db>> {
let mut diags = Vec::new();

// Duplicate parameter names
let dupes = check_duplicate_names(self.params(db).map(|v| v.name(db)), |idxs| {
check_duplicate_names(self.params(db).map(|v| v.name(db)), |idxs| {
TyLowerDiag::DuplicateArgName(self, idxs).into()
});
let found_dupes = !dupes.is_empty();
diags.extend(dupes);

// Duplicate labels (only if names were unique)
if !found_dupes {
diags.extend(check_duplicate_names(
self.params(db).map(|v| v.label_eagerly(db)),
|idxs| TyLowerDiag::DuplicateArgLabel(self, idxs).into(),
));
}

diags
})
.into_iter()
.collect()
}

/// Diagnostics related to the explicit return type (kind/const checks).
Expand Down
6 changes: 3 additions & 3 deletions crates/hir/test_files/ty_check/method.fe
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ impl<T> Option<T> {
}
}

pub fn get_or_insert(mut self, inner t: T) -> T {
pub fn get_or_insert(mut self, inner: T) -> T {
match self {
Self::Some(t) => t
Self::None => {
self = Self::Some(t)
t
self = Self::Some(inner)
inner
}
}
}
Expand Down
Loading