Skip to content

Commit 9db7bdd

Browse files
committed
Fix mismatch between step and tracks_reference_state
1 parent e33bde4 commit 9db7bdd

7 files changed

+63
-241
lines changed

src/rulesets/ty_based.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -140,23 +140,23 @@ impl RuleOptions {
140140
ref_binding_on_inherited,
141141
mut_binding_on_inherited,
142142
} = *self;
143-
if !match_constructor_through_ref {
144-
false
145-
} else if matches!(inherited_ref_on_ref, InheritedRefOnRefBehavior::EatOuter)
146-
&& matches!(
143+
let need_state_for_refs = {
144+
let always_eat_ref_ty =
145+
matches!(inherited_ref_on_ref, InheritedRefOnRefBehavior::EatOuter)
146+
&& eat_inherited_ref_alone;
147+
match_constructor_through_ref && !always_eat_ref_ty
148+
};
149+
let need_state_for_bindings = {
150+
let ignore_state_for_bindings = matches!(
147151
ref_binding_on_inherited,
148152
RefBindingOnInheritedBehavior::AllocTemporary
149-
)
150-
&& matches!(
153+
) && matches!(
151154
mut_binding_on_inherited,
152155
MutBindingOnInheritedBehavior::Keep
153-
)
154-
&& eat_inherited_ref_alone
155-
{
156-
false
157-
} else {
158-
true
159-
}
156+
);
157+
!ignore_state_for_bindings
158+
};
159+
need_state_for_refs || need_state_for_bindings
160160
}
161161
}
162162
}

src/solvers/typing_rules.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,15 @@ pub enum TypeError {
4747

4848
impl<'a> TypingPredicate<'a> {
4949
/// Apply one step of rule to this predicate.
50+
///
5051
/// Note: The `downgrade_mut_inside_shared` option is special: it inspects `self.expr` in
5152
/// non-trivial ways.
5253
/// All the other rules inspect `self.pat`, `self.expr.ty`, `self.expr.binding_mode()` up to a
5354
/// fixed depth. We trigger `OverlyGeneral` errors it the rules needs more information to
5455
/// decide what to do. This is how we can auto-generate the rules listings.
56+
///
57+
/// We take much care to inspect the predicate as little as possible, to make rules as simple
58+
/// as possible.
5559
pub fn step(&self, ctx: TypingCtx<'a>) -> Result<(Rule, Vec<Self>), TypeError> {
5660
use DeepeningRequest as D;
5761
use Pattern as P;
@@ -138,9 +142,12 @@ impl<'a> TypingPredicate<'a> {
138142
let mut reborrow_after = None;
139143

140144
// We only inspect the binding mode if there are options that need it.
141-
let no_inspect_bm =
145+
let always_eat_ref_ty =
142146
matches!(o.inherited_ref_on_ref, InheritedRefOnRefBehavior::EatOuter)
143-
&& (o.eat_inherited_ref_alone || !o.match_constructor_through_ref);
147+
&& o.eat_inherited_ref_alone;
148+
let no_inspect_bm = always_eat_ref_ty
149+
|| (matches!(ctx.type_of_interest, TypeOfInterest::UserVisible)
150+
&& !o.match_constructor_through_ref);
144151
// Construct the dereferenced expression.
145152
let expr = if !no_inspect_bm && let ByRef(bm_mtbl) = self.expr.binding_mode()? {
146153
// The reference is inherited; options differ in their treatment of this case.

tests/snapshots/[email protected]

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ p0 @ e.0: T0, p1 @ e.1: T1
1919
[p0, p1] @ e: [T0, T1]
2020

2121
p @ *e: T
22-
-------------------------------- "Deref"
23-
&p @ e: &T, e is not a reference
22+
---------- "Deref"
23+
&p @ e: &T
2424

2525
p @ *e: T
26-
---------------------------------------- "Deref"
27-
&mut p @ e: &mut T, e is not a reference
26+
------------------ "Deref"
27+
&mut p @ e: &mut T
2828

2929
x @ &e: &T
3030
---------------------------------- "BindingBorrow"

tests/snapshots/[email protected]

+19-24
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,30 @@ info:
1414
ref_binding_on_inherited: Error
1515
mut_binding_on_inherited: Error
1616
---
17-
p0: T0, p1: T1
18-
------------------ "Constructor"
19-
[p0, p1]: [T0, T1]
17+
realp0: T0, real p1: T1
18+
----------------------------- "Constructor"
19+
_[p0, p1]: [T0, T1]
2020

21-
ERROR can't display the following rule with the requested style (NeedToShowReferenceState)
22-
p @ *e: T
23-
-------------------------------- "Deref"
24-
&p @ e: &T, e is not a reference
21+
realp: T
22+
----------- "Deref"
23+
r&p: &T
2524

26-
ERROR can't display the following rule with the requested style (NeedToShowReferenceState)
27-
p @ *e: T
28-
---------------------------------------- "Deref"
29-
&mut p @ e: &mut T, e is not a reference
25+
realp: T
26+
------------------ "Deref"
27+
r&mut p: &mut T
3028

31-
ERROR can't display the following rule with the requested style (NeedToShowReferenceState)
32-
x @ &e: &T
33-
---------------------------------- "BindingBorrow"
34-
ref x @ e: T, e is not a reference
29+
inhx: &T
30+
--------------- "BindingBorrow"
31+
realref x: T
3532

36-
ERROR can't display the following rule with the requested style (NeedToShowReferenceState)
37-
x @ &mut e: &mut T
38-
-------------------------------------- "BindingBorrow"
39-
ref mut x @ e: T, e is not a reference
33+
inhx: &mut T
34+
------------------- "BindingBorrow"
35+
realref mut x: T
4036

4137

42-
---- "Binding"
43-
x: T
38+
-------- "Binding"
39+
rx: T
4440

45-
ERROR can't display the following rule with the requested style (NeedToShowReferenceState)
4641

47-
---------------------------------- "Binding"
48-
mut x @ e: T, e is not a reference
42+
--------------- "Binding"
43+
realmut x: T

tests/snapshots/[email protected]

+10-118
Original file line numberDiff line numberDiff line change
@@ -30,45 +30,9 @@ The old ruleset is on the left, and the new one on the right.
3030
---------------------------- "ConstructorMultiRef" |
3131
r, m ⊢ [p0, p1]: &mut &mut T |
3232

33-
_, rop: T |
34-
------------------------------------- "Deref" |
35-
inh, m&p: &T, T is not a reference |
36-
37-
real, rop: &T |
38-
---------------- "Deref" |
39-
inh, m&p: &&T |
40-
41-
real, rop: &mut T |
42-
-------------------- "Deref" |
43-
inh, m&p: &&mut T |
44-
45-
_, mp: T |
46-
--------------------------------------------- "Deref" |
47-
inh, m&mut p: &mut T, T is not a reference |
48-
49-
real, mp: &T |
50-
------------------------ "Deref" |
51-
inh, m&mut p: &mut &T |
52-
53-
real, mp: &mut T |
54-
---------------------------- "Deref" |
55-
inh, m&mut p: &mut &mut T |
56-
5733
real, rop: T |
58-
-------------------- "DerefMutWithShared" |
59-
real, m&p: &mut T |
60-
61-
_, rop: T |
62-
----------------------------------------- "DerefMutWithShared" |
63-
inh, m&p: &mut T, T is not a reference |
64-
65-
real, rop: &T |
66-
-------------------- "DerefMutWithShared" |
67-
inh, m&p: &mut &T |
68-
69-
real, rop: &mut T |
70-
------------------------ "DerefMutWithShared" |
71-
inh, m&p: &mut &mut T |
34+
----------------- "DerefMutWithShared" |
35+
r, m&p: &mut T |
7236

7337
|
7438
------------------ "Binding" |
@@ -117,45 +81,9 @@ The old current ruleset is on the left, and the new current one on the right.
11781
| ---------------------------- "ConstructorMultiRef"
11882
| r, m ⊢ [p0, p1]: &mut &mut T
11983

120-
| _, rop: T
121-
| ------------------------------------- "Deref"
122-
| inh, m&p: &T, T is not a reference
123-
124-
| real, rop: &T
125-
| ---------------- "Deref"
126-
| inh, m&p: &&T
127-
128-
| real, rop: &mut T
129-
| -------------------- "Deref"
130-
| inh, m&p: &&mut T
131-
132-
| _, mp: T
133-
| --------------------------------------------- "Deref"
134-
| inh, m&mut p: &mut T, T is not a reference
135-
136-
| real, mp: &T
137-
| ------------------------ "Deref"
138-
| inh, m&mut p: &mut &T
139-
140-
| real, mp: &mut T
141-
| ---------------------------- "Deref"
142-
| inh, m&mut p: &mut &mut T
143-
14484
| real, rop: T
145-
| -------------------- "DerefMutWithShared"
146-
| real, m&p: &mut T
147-
148-
| _, rop: T
149-
| ----------------------------------------- "DerefMutWithShared"
150-
| inh, m&p: &mut T, T is not a reference
151-
152-
| real, rop: &T
153-
| -------------------- "DerefMutWithShared"
154-
| inh, m&p: &mut &T
155-
156-
| real, rop: &mut T
157-
| ------------------------ "DerefMutWithShared"
158-
| inh, m&p: &mut &mut T
85+
| ----------------- "DerefMutWithShared"
86+
| r, m&p: &mut T
15987

16088
|
16189
| ------------------ "Binding"
@@ -209,52 +137,16 @@ The current ruleset is on the left, and the saved one on the right.
209137
r, m ⊢ [p0, p1]: &mut &mut T |
210138

211139
real, rop: T | real, rop: T
212-
---------------- "Deref" | ---------------- "Deref"
213-
real, m&p: &T | real, m&p: &T
214-
215-
_, rop: T |
216-
------------------------------------- "Deref" |
217-
inh, m&p: &T, T is not a reference |
218-
219-
real, rop: &T |
220-
---------------- "Deref" |
221-
inh, m&p: &&T |
222-
223-
real, rop: &mut T |
224-
-------------------- "Deref" |
225-
inh, m&p: &&mut T |
140+
--------------- "Deref" | --------------- "Deref"
141+
r, m&p: &T | r, m&p: &T
226142

227143
real, mp: T | real, mp: T
228-
------------------------ "Deref" | ------------------------ "Deref"
229-
real, m&mut p: &mut T | real, m&mut p: &mut T
230-
231-
_, mp: T |
232-
--------------------------------------------- "Deref" |
233-
inh, m&mut p: &mut T, T is not a reference |
234-
235-
real, mp: &T |
236-
------------------------ "Deref" |
237-
inh, m&mut p: &mut &T |
238-
239-
real, mp: &mut T |
240-
---------------------------- "Deref" |
241-
inh, m&mut p: &mut &mut T |
144+
--------------------- "Deref" | --------------------- "Deref"
145+
r, m&mut p: &mut T | r, m&mut p: &mut T
242146

243147
real, rop: T |
244-
-------------------- "DerefMutWithShared" |
245-
real, m&p: &mut T |
246-
247-
_, rop: T |
248-
----------------------------------------- "DerefMutWithShared" |
249-
inh, m&p: &mut T, T is not a reference |
250-
251-
real, rop: &T |
252-
-------------------- "DerefMutWithShared" |
253-
inh, m&p: &mut &T |
254-
255-
real, rop: &mut T |
256-
------------------------ "DerefMutWithShared" |
257-
inh, m&p: &mut &mut T |
148+
----------------- "DerefMutWithShared" |
149+
r, m&p: &mut T |
258150

259151
inh, mx: &T | inh, mx: &T
260152
------------------ "BindingBorrow" | ------------------ "BindingBorrow"

0 commit comments

Comments
 (0)