Skip to content

Commit

Permalink
Fix lalrpop#480 ('use super::' with internal lexer)
Browse files Browse the repository at this point in the history
I was surprised to see that the infrastructure was already there,
and in fact the correct function was even already in use!
Just this argument was wrong.

I found it surprising that this would be the case, so I did some sleuthing
into the commit history. The single line modified by this commit was written
in this commit: 1b206d1

This line was probably copied from the similar line that generates the
'uses' at the very top of the generated grammar file.

You can see the test added for the aforementioned commit here:

lalrpop@4ecd594#diff-a6e59d678291fca1872fd2212d565242df94e2bf23428bc5135a3a8c7e7bc250

That test had a very simple import of the form `use super::Name;`.
This worked, but for the wrong reason; it was causing the internal
module to import `Name` indirectly via the `use super::Name` that was
generated in the outer module.  Anything of the form
`use super::module::Name` or `use super::Name as Renamed;' would fail,
however, as `module` and `Name` wouldn't exist at that location.
  • Loading branch information
ExpHP committed Apr 13, 2021
1 parent d35807f commit d27b8e4
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
11 changes: 11 additions & 0 deletions lalrpop-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ lalrpop_mod!(mut_name);
/// test that uses `super` in paths in various places
lalrpop_mod!(use_super);

/// regression test for #480 (`use super` with default tokenizer)
lalrpop_mod!(use_super_internal_tok);

/// Custom error type (issue #113)
#[derive(Debug, PartialEq)]
pub struct MyCustomError(char);
Expand Down Expand Up @@ -496,6 +499,14 @@ fn use_super_test1() {
util::test(|v| use_super::SParser::new().parse(v), "()", 0);
}

#[test]
fn use_super_internal_tok() {
assert_eq!(
use_super_internal_tok::SParser::new().parse("b").unwrap(),
util::CaptureMe,
);
}

#[test]
fn error_test1() {
match util::test_err_gen(|t| error::ItemsParser::new().parse(t), "---+") {
Expand Down
5 changes: 5 additions & 0 deletions lalrpop-test/src/use_super_internal_tok.lalrpop
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
grammar;

use super::util::CaptureMe as Renamed;

pub S: Renamed = "b" => Renamed;
4 changes: 4 additions & 0 deletions lalrpop-test/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,7 @@ pub fn compare_str(actual: &str, expected: &str, msg: &str) {
panic!("{}", msg);
}
}

/// Something that a test can import if it's trying specifically to test imports.
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub struct CaptureMe;
2 changes: 1 addition & 1 deletion lalrpop/src/lexer/intern_token/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn compile<W: Write>(
rust!(out, "#[cfg_attr(rustfmt, rustfmt_skip)]");
rust!(out, "mod {}intern_token {{", prefix);
rust!(out, "#![allow(unused_imports)]");
out.write_uses("", &grammar)?;
out.write_uses("super::", &grammar)?;
rust!(
out,
"pub fn new_builder() -> {}lalrpop_util::lexer::MatcherBuilder {{",
Expand Down

0 comments on commit d27b8e4

Please sign in to comment.