Skip to content

Commit fd16689

Browse files
committed
duplicate definition error fixup
1 parent cbf0ec5 commit fd16689

File tree

5 files changed

+29
-14
lines changed

5 files changed

+29
-14
lines changed

baml_language/crates/baml_diagnostics/src/compiler_error/error_format.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,19 @@ where
8989
name,
9090
kind,
9191
first,
92+
first_path,
9293
second,
94+
second_path,
9395
} => (
9496
Report::build(ReportKind::Error, second)
9597
.with_message(format!("Duplicate {kind} '{name}'"))
9698
.with_label(
97-
Label::new(second).with_message(format!("{kind} '{name}' defined here")),
99+
Label::new(second)
100+
.with_message(format!("{kind} '{name}' defined in {second_path}")),
98101
)
99102
.with_label(
100-
Label::new(first).with_message(format!("'{name}' previously defined here")),
103+
Label::new(first)
104+
.with_message(format!("'{name}' previously defined in {first_path}")),
101105
),
102106
DUPLICATE_NAME,
103107
),

baml_language/crates/baml_diagnostics/src/compiler_error/name_error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ pub enum NameError {
1212
name: String,
1313
kind: &'static str,
1414
first: Span,
15+
first_path: String,
1516
second: Span,
17+
second_path: String,
1618
},
1719
}

baml_language/crates/baml_hir/src/lib.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,7 @@ use rustc_hash::FxHashMap;
715715
struct ItemInfo {
716716
kind: &'static str,
717717
span: Span,
718+
path: String,
718719
}
719720

720721
/// Validate that there are no duplicate names in the project.
@@ -727,48 +728,54 @@ pub fn validate_duplicate_names(db: &dyn Db, root: baml_workspace::Project) -> V
727728
let mut errors = Vec::new();
728729

729730
for item in items.items(db) {
730-
let (name, kind, span) = match item {
731+
let (name, kind, span, path) = match item {
731732
ItemId::Function(loc) => {
732733
let file = loc.file(db);
733734
let item_tree = file_item_tree(db, file);
734735
let func = &item_tree[loc.id(db)];
735736
let span = Span::new(file.file_id(db), TextRange::empty(0.into()));
736-
(func.name.clone(), "function", span)
737+
let path = file.path(db).display().to_string();
738+
(func.name.clone(), "function", span, path)
737739
}
738740
ItemId::Class(loc) => {
739741
let file = loc.file(db);
740742
let item_tree = file_item_tree(db, file);
741743
let class = &item_tree[loc.id(db)];
742744
let span = Span::new(file.file_id(db), TextRange::empty(0.into()));
743-
(class.name.clone(), "class", span)
745+
let path = file.path(db).display().to_string();
746+
(class.name.clone(), "class", span, path)
744747
}
745748
ItemId::Enum(loc) => {
746749
let file = loc.file(db);
747750
let item_tree = file_item_tree(db, file);
748751
let enum_def = &item_tree[loc.id(db)];
749752
let span = Span::new(file.file_id(db), TextRange::empty(0.into()));
750-
(enum_def.name.clone(), "enum", span)
753+
let path = file.path(db).display().to_string();
754+
(enum_def.name.clone(), "enum", span, path)
751755
}
752756
ItemId::TypeAlias(loc) => {
753757
let file = loc.file(db);
754758
let item_tree = file_item_tree(db, file);
755759
let alias = &item_tree[loc.id(db)];
756760
let span = Span::new(file.file_id(db), TextRange::empty(0.into()));
757-
(alias.name.clone(), "type alias", span)
761+
let path = file.path(db).display().to_string();
762+
(alias.name.clone(), "type alias", span, path)
758763
}
759764
ItemId::Client(loc) => {
760765
let file = loc.file(db);
761766
let item_tree = file_item_tree(db, file);
762767
let client = &item_tree[loc.id(db)];
763768
let span = Span::new(file.file_id(db), TextRange::empty(0.into()));
764-
(client.name.clone(), "client", span)
769+
let path = file.path(db).display().to_string();
770+
(client.name.clone(), "client", span, path)
765771
}
766772
ItemId::Test(loc) => {
767773
let file = loc.file(db);
768774
let item_tree = file_item_tree(db, file);
769775
let test = &item_tree[loc.id(db)];
770776
let span = Span::new(file.file_id(db), TextRange::empty(0.into()));
771-
(test.name.clone(), "test", span)
777+
let path = file.path(db).display().to_string();
778+
(test.name.clone(), "test", span, path)
772779
}
773780
};
774781

@@ -777,10 +784,12 @@ pub fn validate_duplicate_names(db: &dyn Db, root: baml_workspace::Project) -> V
777784
name: name.to_string(),
778785
kind,
779786
first: existing.span,
787+
first_path: existing.path.clone(),
780788
second: span,
789+
second_path: path,
781790
});
782791
} else {
783-
seen.insert(name, ItemInfo { kind, span });
792+
seen.insert(name, ItemInfo { kind, span, path });
784793
}
785794
}
786795

baml_language/crates/baml_tests/snapshots/parser_constructors/baml_tests__parser_constructors__05_diagnostics.snap

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

baml_language/crates/baml_tests/snapshots/parser_stress/baml_tests__parser_stress__05_diagnostics.snap

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)