Skip to content

Commit

Permalink
fix(frontends/lean/builtin_cmds): add modifiers/attrs/docstring check…
Browse files Browse the repository at this point in the history
… to `local` command (#711)

The notation commands are supposed to reject modifiers/attrs/docstrings, but using them via the `local` command circumvents the check. This adds the check for `local` notations.
  • Loading branch information
kmill committed Apr 29, 2022
1 parent 5885f62 commit 5d73a87
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/frontends/lean/builtin_cmds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ static environment local_cmd(parser & p, ast_id & cmd_id, cmd_meta const & meta)
p.next();
return local_attribute_cmd(p, cmd_id, meta);
} else {
meta.throw_exception_if_nonempty();
return local_notation_cmd(p, cmd_id);
}
}
Expand Down
16 changes: 10 additions & 6 deletions src/frontends/lean/cmd_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ struct cmd_meta {
cmd_meta(decl_attributes const & attrs, decl_modifiers const & mods,
optional<std::string> const & doc = optional<std::string>()):
m_attrs(attrs), m_modifiers(mods), m_doc_string(doc) {}

void throw_exception_if_nonempty() const & {
if (m_modifiers)
throw exception("command does not accept modifiers");
if (m_attrs)
throw exception("command does not accept attributes");
if (m_doc_string)
throw exception("command does not accept doc string");
}
};

typedef std::function<environment(parser&, ast_id &, cmd_meta const &)> command_fn;
Expand All @@ -39,12 +48,7 @@ struct cmd_info_tmpl {
m_name(n), m_descr(d), m_fn(fn), m_skip_token(skip_token) {}
cmd_info_tmpl(name const & n, char const * d, std::function<environment(parser&, ast_id&)> const & fn, bool skip_token = true):
cmd_info_tmpl(n, d, [=](parser & p, ast_id & cmd_id, cmd_meta const & meta) {
if (meta.m_modifiers)
throw exception("command does not accept modifiers");
if (meta.m_attrs)
throw exception("command does not accept attributes");
if (meta.m_doc_string)
throw exception("command does not accept doc string");
meta.throw_exception_if_nonempty();
return fn(p, cmd_id);
}, skip_token) {}
cmd_info_tmpl() {}
Expand Down
5 changes: 5 additions & 0 deletions tests/lean/local_notation_meta_bug.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
local infix ` + ` := nat.add
@[class] local infix ` + ` := nat.add
noncomputable local infix ` + ` := nat.add
@[class] noncomputable local infix ` + ` := nat.add
/-- foo -/ local infix ` + ` := nat.add
4 changes: 4 additions & 0 deletions tests/lean/local_notation_meta_bug.lean.expected.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
local_notation_meta_bug.lean:2:9: error: command does not accept attributes
local_notation_meta_bug.lean:3:14: error: command does not accept modifiers
local_notation_meta_bug.lean:4:23: error: command does not accept modifiers
local_notation_meta_bug.lean:5:11: error: command does not accept doc string

0 comments on commit 5d73a87

Please sign in to comment.