Skip to content

Commit

Permalink
use reduction prefix to decide between variants
Browse files Browse the repository at this point in the history
We used to also consider precedence, but we are moving away from that
approach. Remove some tests that no longer test a relevant edge case.
  • Loading branch information
nikomatsakis committed Nov 7, 2023
1 parent 165cb7c commit 2b49e23
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 71 deletions.
4 changes: 1 addition & 3 deletions crates/formality-core/src/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,7 @@ where
l1.len() > l2.len() && (0..l2.len()).all(|i| l1[i] == l2[i])
}

s_i.precedence.level > s_j.precedence.level
|| (s_i.precedence.level == s_j.precedence.level
&& has_prefix(&s_i.reductions, &s_j.reductions))
has_prefix(&s_i.reductions, &s_j.reductions)
}
}

Expand Down
68 changes: 0 additions & 68 deletions tests/parser-torture-tests/precedence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,71 +59,3 @@ fn equal_precedence_panics() {
"#]]
.assert_debug_eq(&term);
}

#[test]
#[should_panic(expected = "extra tokens")]
fn higher_precedence_less_reductions_1() {
// Subtle: In this case, A has higher precedence,
// so even though we COULD parse the entire string,
// we prefer to just parse the first identifier,
// which results in a panic.
//
// In the past, we had a bug in our preference function
// that caused it to be order dependent, sometimes
// panicking and sometimes not. Hence we have a similar
// test with opposite order.

#[term]
pub enum Root {
#[cast]
#[precedence(1)]
A(A),

#[cast]
B(B),
}

#[term($v0)]
pub struct A(Id);

#[term($v0 $v1)]
pub struct B(A, Id);

formality_core::id!(Id);

let term: Root = crate::ptt::term("my String");
expect_test::expect![[r#"
"#]]
.assert_debug_eq(&term);
}

#[test]
#[should_panic(expected = "extra tokens")]
fn higher_precedence_less_reductions_2() {
// Same as `higher_precedence_less_reductions_1` but with
// opposite term order. See explanation in that function.

#[term]
pub enum Root {
#[cast]
B(B),

#[cast]
#[precedence(1)]
A(A),
}

#[term($v0)]
pub struct A(Id);

#[term($v0 $v1)]
pub struct B(A, Id);

formality_core::id!(Id);

let term: Root = crate::ptt::term("my String");
expect_test::expect![[r#"
my String
"#]]
.assert_debug_eq(&term);
}

0 comments on commit 2b49e23

Please sign in to comment.