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
8 changes: 2 additions & 6 deletions crates/plotnik-lib/src/diagnostics/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ impl DiagnosticKind {
Self::DuplicateDefinition => "name already defined",
Self::UndefinedReference => "undefined reference",
Self::MixedAltBranches => "cannot mix labeled and unlabeled branches",
Self::RecursionNoEscape => "infinite recursion detected",
Self::DirectRecursion => "direct recursion: query will stuck without matching anything",
Self::RecursionNoEscape => "infinite recursion: cycle has no escape path",
Self::DirectRecursion => "infinite recursion: cycle consumes no input",
Self::FieldSequenceValue => "field must match exactly one node",

// Link pass - grammar validation
Expand Down Expand Up @@ -200,10 +200,6 @@ impl DiagnosticKind {
Self::InvalidFieldChildType => "node type `{}` is not valid for this field".to_string(),
Self::InvalidChildType => "`{}` cannot be a child of this node".to_string(),

// Recursion with cycle path
Self::RecursionNoEscape => "infinite recursion: {}".to_string(),
Self::DirectRecursion => "direct recursion: {}".to_string(),

// Alternation mixing
Self::MixedAltBranches => "cannot mix labeled and unlabeled branches: {}".to_string(),

Expand Down
16 changes: 8 additions & 8 deletions crates/plotnik-lib/src/query/link_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -929,12 +929,12 @@ fn ref_followed_recursive_with_invalid_type() {
|
help: valid types for `name`: `identifier`

error: direct recursion: cycle `Foo` → `Foo` will stuck without matching anything
error: infinite recursion: cycle consumes no input
|
1 | Foo = [(number) (Foo)]
| ^^^
| |
| `Foo` references itself
| references itself
");
}

Expand All @@ -950,12 +950,12 @@ fn ref_followed_recursive_valid() {

assert!(!query.is_valid());
insta::assert_snapshot!(query.dump_diagnostics(), @r"
error: direct recursion: cycle `Foo` → `Foo` will stuck without matching anything
error: infinite recursion: cycle consumes no input
|
1 | Foo = [(identifier) (Foo)]
| ^^^
| |
| `Foo` references itself
| references itself
");
}

Expand Down Expand Up @@ -991,15 +991,15 @@ fn ref_followed_mutual_recursion() {
|
help: valid types for `name`: `identifier`

error: direct recursion: cycle `Bar` → `Foo` → `Bar` will stuck without matching anything
error: infinite recursion: cycle consumes no input
|
1 | Foo = [(number) (Bar)]
| --- `Foo` references `Bar` (completing cycle)
| --- references Bar (completing cycle)
2 | Bar = [(string) (Foo)]
| --- ^^^
| | |
| | `Bar` references `Foo`
| `Bar` is defined here
| | references Foo
| Bar is defined here
");
}

Expand Down
41 changes: 10 additions & 31 deletions crates/plotnik-lib/src/query/recursion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ impl Query<'_> {

fn build_self_ref_chain(&self, name: &str) -> Vec<(TextRange, String)> {
self.find_reference_location(name, name)
.map(|range| vec![(range, format!("`{}` references itself", name))])
.map(|range| vec![(range, format!("{} references itself", name))])
.unwrap_or_default()
}

Expand Down Expand Up @@ -253,9 +253,9 @@ impl Query<'_> {
let to = &path[(i + 1) % path.len()];
self.find_reference_location(from, to).map(|range| {
let msg = if i == path.len() - 1 {
format!("`{}` references `{}` (completing cycle)", from, to)
format!("references {} (completing cycle)", to)
} else {
format!("`{}` references `{}`", from, to)
format!("references {}", to)
};
(range, msg)
})
Expand All @@ -267,7 +267,7 @@ impl Query<'_> {
if cycle.len() == 1 {
return self
.find_unguarded_reference_location(&cycle[0], &cycle[0])
.map(|range| vec![(range, format!("`{}` references itself", cycle[0]))])
.map(|range| vec![(range, "references itself".to_string())])
.unwrap_or_default();
}
self.build_chain_generic(cycle, |from, to| {
Expand All @@ -286,9 +286,9 @@ impl Query<'_> {
let to = &path_nodes[(i + 1) % path_nodes.len()];
find_loc(from, to).map(|range| {
let msg = if i == path_nodes.len() - 1 {
format!("`{}` references `{}` (completing cycle)", from, to)
format!("references {} (completing cycle)", to)
} else {
format!("`{}` references `{}`", from, to)
format!("references {}", to)
};
(range, msg)
})
Expand All @@ -302,14 +302,6 @@ impl Query<'_> {
scc: &[String],
related: Vec<(TextRange, String)>,
) {
let cycle_str = if scc.len() == 1 {
format!("`{}` → `{}`", primary_name, primary_name)
} else {
let mut cycle: Vec<_> = scc.iter().map(|s| format!("`{}`", s)).collect();
cycle.push(format!("`{}`", scc[0]));
cycle.join(" → ")
};

let range = related
.first()
.map(|(r, _)| *r)
Expand All @@ -325,15 +317,14 @@ impl Query<'_> {

let mut builder = self
.recursion_diagnostics
.report(DiagnosticKind::RecursionNoEscape, range)
.message(format!("cycle {} has no escape path", cycle_str));
.report(DiagnosticKind::RecursionNoEscape, range);

for (rel_range, rel_msg) in related {
builder = builder.related_to(rel_msg, rel_range);
}

if let Some(range) = def_range {
builder = builder.related_to(format!("`{}` is defined here", primary_name), range);
builder = builder.related_to(format!("{} is defined here", primary_name), range);
}

builder.emit();
Expand All @@ -344,14 +335,6 @@ impl Query<'_> {
scc: &[String],
related: Vec<(TextRange, String)>,
) {
let cycle_str = if scc.len() == 1 {
format!("`{}` → `{}`", primary_name, primary_name)
} else {
let mut cycle: Vec<_> = scc.iter().map(|s| format!("`{}`", s)).collect();
cycle.push(format!("`{}`", scc[0]));
cycle.join(" → ")
};

let range = related
.first()
.map(|(r, _)| *r)
Expand All @@ -367,18 +350,14 @@ impl Query<'_> {

let mut builder = self
.recursion_diagnostics
.report(DiagnosticKind::DirectRecursion, range)
.message(format!(
"cycle {} will stuck without matching anything",
cycle_str
));
.report(DiagnosticKind::DirectRecursion, range);

for (rel_range, rel_msg) in related {
builder = builder.related_to(rel_msg, rel_range);
}

if let Some(range) = def_range {
builder = builder.related_to(format!("`{}` is defined here", primary_name), range);
builder = builder.related_to(format!("{} is defined here", primary_name), range);
}

builder.emit();
Expand Down
Loading