Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add binders validator #694

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions chalk-ir/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,15 @@ where
}
}

/// Invoked for each occurence of a `Binders`, before visiting them.
fn before_binders(&mut self, _kinds: &crate::VariableKinds<I>) {}
/// Invoked for each occurence of a `Canonical`, before visiting them.
fn before_canonical(&mut self, _kinds: &crate::CanonicalVarKinds<I>) {}
/// Invoked for each occurence of a `FnPointer`, before visiting them.
fn before_fn_pointer_substs(&mut self, _number: usize) {}
/// Invoked for each occurence of a type introducing binders, after visiting them.
fn after_any_binders(&mut self) {}

Comment on lines +231 to +239
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it's worth just adding a visit_binders function like in rustc? (Probably) Is it all helpful to have these split out? I don't know if rustc calls visit_binder for fn pointer substs or canonical items, but we should.

Unfortunately, I think that our visit_binders would have to look like fn visit_binder<T: SuperVisit<I>>(&mut self, binders: crate::VariableKinds<I>, value: T) -> ControlFlow<Self::BreakTy>);, but that's probably fine.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, we could just change this to only have before_binders and do the "into VariableKinds conversion" happen before calling it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried something like that first, the problem is that it makes the Visitor trait not object safe.

We could do the before_binders, but it'd mean doing the conversion in every visitor instead of just the one where it's necessary.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I mean, you only need to do the conversion in the Visit impls, right?

/// Gets the visitor's interner.
fn interner(&self) -> &'i I;
}
Expand Down
20 changes: 15 additions & 5 deletions chalk-ir/src/visit/binder_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@ impl<I: Interner> Visit<I> for FnPointer<I> {
where
I: 'i,
{
self.substitution
.visit_with(visitor, outer_binder.shifted_in())
visitor.before_fn_pointer_substs(self.num_binders);
let result = self
.substitution
.visit_with(visitor, outer_binder.shifted_in());
visitor.after_any_binders();
result
}
}

impl<T, I: Interner> Visit<I> for Binders<T>
where
T: HasInterner + Visit<I>,
T: HasInterner<Interner = I> + Visit<I>,
{
fn visit_with<'i, B>(
&self,
Expand All @@ -32,7 +36,10 @@ where
where
I: 'i,
{
self.value.visit_with(visitor, outer_binder.shifted_in())
visitor.before_binders(&self.binders);
let result = self.value.visit_with(visitor, outer_binder.shifted_in());
visitor.after_any_binders();
result
}
}

Expand All @@ -49,6 +56,9 @@ where
where
I: 'i,
{
self.value.visit_with(visitor, outer_binder.shifted_in())
visitor.before_canonical(&self.binders);
let result = self.value.visit_with(visitor, outer_binder.shifted_in());
visitor.after_any_binders();
result
}
}
130 changes: 130 additions & 0 deletions chalk-ir/src/visit/visitors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ pub trait VisitExt<I: Interner>: Visit<I> {
)
.is_break()
}

/// Validates binders.
fn validate_binders(&self, interner: &I) -> bool {
self.visit_with(
&mut binders_check::BindersValidator {
interner,
stack: Vec::new(),
},
DebruijnIndex::INNERMOST,
)
.is_continue()
}
}

impl<T, I: Interner> VisitExt<I> for T where T: Visit<I> {}
Expand Down Expand Up @@ -39,3 +51,121 @@ impl<'i, I: Interner> Visitor<'i, I> for FindFreeVarsVisitor<'i, I> {
ControlFlow::BREAK
}
}

mod binders_check {
use crate::{
interner::Interner,
visit::{ControlFlow, SuperVisit, Visitor},
Const, DebruijnIndex, Lifetime, Ty, VariableKind,
};

pub struct BindersValidator<'i, I: Interner> {
pub(crate) interner: &'i I,
pub(crate) stack: Vec<Vec<VariableKind<I>>>,
}

impl<'i, I: Interner> Visitor<'i, I> for BindersValidator<'i, I> {
type BreakTy = ();

fn as_dyn(&mut self) -> &mut dyn Visitor<'i, I, BreakTy = Self::BreakTy> {
self
}

fn interner(&self) -> &'i I {
self.interner
}

fn visit_ty(
&mut self,
ty: &Ty<I>,
outer_binder: DebruijnIndex,
) -> ControlFlow<Self::BreakTy> {
if let Some(bv) = ty.bound_var(self.interner) {
assert_eq!(self.stack.len(), outer_binder.depth() as usize);
if bv.debruijn < outer_binder {
let kinds = &self.stack[self.stack.len() - 1 - bv.debruijn.depth() as usize];
match kinds.get(bv.index) {
Some(VariableKind::Ty(_)) => {}
_ => {
return ControlFlow::BREAK;
}
}
} else {
return ControlFlow::BREAK;
}
}
ty.super_visit_with(self.as_dyn(), outer_binder)
}

fn visit_const(
&mut self,
constant: &Const<I>,
outer_binder: DebruijnIndex,
) -> ControlFlow<Self::BreakTy> {
if let Some(bv) = constant.bound_var(self.interner) {
assert_eq!(self.stack.len(), outer_binder.depth() as usize);
if bv.debruijn < outer_binder {
let kinds = &self.stack[self.stack.len() - 1 - bv.debruijn.depth() as usize];
match kinds.get(bv.index) {
Some(VariableKind::Const(_ty)) => {
// FIXME: validate that type can match?
}
_ => {
return ControlFlow::BREAK;
}
}
} else {
return ControlFlow::BREAK;
}
}
constant.super_visit_with(self.as_dyn(), outer_binder)
}

fn visit_lifetime(
&mut self,
lifetime: &Lifetime<I>,
outer_binder: DebruijnIndex,
) -> ControlFlow<Self::BreakTy> {
if let Some(bv) = lifetime.bound_var(self.interner) {
assert_eq!(self.stack.len(), outer_binder.depth() as usize);
if bv.debruijn < outer_binder {
let kinds = &self.stack[self.stack.len() - 1 - bv.debruijn.depth() as usize];
match kinds.get(bv.index) {
Some(VariableKind::Lifetime) => {}
_ => {
return ControlFlow::BREAK;
}
}
} else {
return ControlFlow::BREAK;
}
}
lifetime.super_visit_with(self.as_dyn(), outer_binder)
}

fn before_binders(&mut self, kinds: &crate::VariableKinds<I>) {
self.stack.push(kinds.as_slice(self.interner).to_vec());
}

fn before_canonical(&mut self, kinds: &crate::CanonicalVarKinds<I>) {
self.stack.push(
kinds
.iter(self.interner)
.map(|wk| wk.kind.clone())
.collect(),
);
}

fn before_fn_pointer_substs(&mut self, number: usize) {
self.stack.push(
std::iter::repeat_with(|| VariableKind::Lifetime)
.take(number)
.collect(),
)
}

fn after_any_binders(&mut self) {
self.stack.pop().unwrap();
}
}
}
4 changes: 2 additions & 2 deletions chalk-solve/src/clauses/builtin_traits/unsize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl<'a, I: Interner> Visitor<'a, I> for UnsizeParameterCollector<'a, I> {

fn outer_binder_parameters_used<I: Interner>(
interner: &I,
v: &Binders<impl Visit<I> + HasInterner>,
v: &Binders<impl Visit<I> + HasInterner<Interner = I>>,
) -> HashSet<usize> {
let mut visitor = UnsizeParameterCollector {
interner,
Expand Down Expand Up @@ -123,7 +123,7 @@ impl<'a, 'p, I: Interner> Visitor<'a, I> for ParameterOccurenceCheck<'a, 'p, I>

fn uses_outer_binder_params<I: Interner>(
interner: &I,
v: &Binders<impl Visit<I> + HasInterner>,
v: &Binders<impl Visit<I> + HasInterner<Interner = I>>,
parameters: &HashSet<usize>,
) -> bool {
let mut visitor = ParameterOccurenceCheck {
Expand Down