Skip to content

Commit

Permalink
fix a bug with signature comments in methods
Browse files Browse the repository at this point in the history
  • Loading branch information
kaikalii committed Jan 12, 2025
1 parent a82dd47 commit 4b1d596
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 40 deletions.
50 changes: 31 additions & 19 deletions src/compile/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,31 +314,43 @@ impl Compiler {
global_index: local.index,
});
let no_code_words = binding.words.iter().all(|w| !w.value.is_code());
let node = if let Some((def, _)) = &data_def {
self.in_method(def, |comp| comp.words(binding.words))
let compile = |comp: &mut Compiler| -> UiuaResult<Node> {
// Compile the words
let node = comp.words(binding.words);
// Add an error binding if there was an error
let mut node = match node {
Ok(node) => node,
Err(e) => {
comp.asm.add_binding_at(
local,
BindingKind::Error,
Some(span.clone()),
meta.clone(),
);
return Err(e);
}
};
// Apply the signature comment
if let Some(comment_sig) = meta.comment.as_ref().and_then(|c| c.sig.as_ref()) {
comp.apply_node_comment(
&mut node,
comment_sig,
&format!("{name}'s"),
&binding.name.span,
);
}
Ok(node)
};
// We may need to compile the words in the context of a data definition method
let mut node = if let Some((def, _)) = &data_def {
self.in_method(def, compile)?
} else {
self.words(binding.words)
compile(self)?
};
let self_referenced = self.current_bindings.pop().unwrap().recurses > 0;
let mut node = match node {
Ok(node) => node,
Err(e) => {
self.asm
.add_binding_at(local, BindingKind::Error, Some(span.clone()), meta);
return Err(e);
}
};
let is_obverse = node
.iter()
.any(|n| matches!(n, Node::CustomInverse(cust, _) if cust.is_obverse));
if let Some(comment_sig) = meta.comment.as_ref().and_then(|c| c.sig.as_ref()) {
self.apply_node_comment(
&mut node,
comment_sig,
&format!("{name}'s"),
&binding.name.span,
);
}

// Normalize external
if external {
Expand Down
22 changes: 5 additions & 17 deletions src/compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -796,8 +796,7 @@ code:
if let Some(sig) = meta.comment.as_ref().and_then(|c| c.sig.as_ref()) {
self.emit_diagnostic(
format!(
"{}'s comment describes {}, but it is a constant",
name,
"{name}'s comment describes {}, but it is a constant",
sig.sig_string(),
),
DiagnosticKind::Warning,
Expand Down Expand Up @@ -949,24 +948,10 @@ code:
// Compile a line, checking an end-of-line signature comment
fn line(&mut self, line: Vec<Sp<Word>>, must_be_callable: bool) -> UiuaResult<Node> {
let comment_sig = line_sig(&line);
let is_empty = line.iter().all(|w| !w.value.is_code());
// Actually compile the line
let mut node = self.words(line)?;
// Validate line signature
if let Some(comment_sig) = comment_sig {
if let Ok(sig) = node.sig() {
if !is_empty && !comment_sig.value.matches_sig(sig) {
self.emit_diagnostic(
format!(
"Line comment describes {}, \
but its code has signature {sig}",
comment_sig.value.sig_string()
),
DiagnosticKind::Warning,
comment_sig.span.clone(),
);
}
}
self.apply_node_comment(&mut node, &comment_sig.value, "Line", &comment_sig.span);
}
// Validate callability
Expand Down Expand Up @@ -999,7 +984,10 @@ code:
) {
let mut spandex: Option<usize> = None;
// Validate comment signature
if let Ok(sig) = node.sig() {
if let Ok(mut sig) = node.sig() {
if let ScopeKind::Method(_) = self.scope.kind {
sig.args += 1;
}
if !comment_sig.matches_sig(sig) {
let span = *spandex.get_or_insert_with(|| self.add_span(span.clone()));
self.emit_diagnostic(
Expand Down
8 changes: 4 additions & 4 deletions tests/data_defs.ua
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
|Foo {Bar Baz}
|Qux [x y z]
|Sed {M N}
|Wir
|Wir

Format ← ⍣(
$"_ and _" °Foo
| $"⟨_ _ _⟩" °Qux
Expand Down Expand Up @@ -57,7 +57,7 @@ S~PushB
┌─╴Foo
|Bar [A]
|Baz [A]
|Qux
|Qux
└─╴
⍤⤙≍ 1 /↥⌕"Non-boxed variant" ⍣Foo~Bar⋅∘ @a
⍤⤙≍ 1 /↥⌕"Non-boxed variant" ⍣Foo~Baz⋅∘ [1 2 3]
Expand All @@ -71,7 +71,7 @@ S~PushB
┌─╴Foo
~{Bar Baz}
~AddBar ← +Bar
~Sum ← AddBar Baz
~Sum ← AddBar Baz # ? Foo
~WithBarSum ← Foo~Sum New 10 Baz
~WithBar ← Self °Bar
~IncrBar ← Self⍜Bar+₁
Expand Down

0 comments on commit 4b1d596

Please sign in to comment.