-
Notifications
You must be signed in to change notification settings - Fork 755
Allow visits from both visitor and VisitorMut at the same time without using trait disambiguation #2389
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
Open
docteurklein
wants to merge
1
commit into
apache:main
Choose a base branch
from
docteurklein:visit-and-visit_mut
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+51
−24
Open
Allow visits from both visitor and VisitorMut at the same time without using trait disambiguation #2389
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -59,7 +59,7 @@ pub trait VisitMut { | |
| /// Implementations should call the appropriate mutable visitor hooks to | ||
| /// traverse and allow in-place mutation of child nodes. Returning a | ||
| /// `ControlFlow` value permits early termination of the traversal. | ||
| fn visit<V: VisitorMut>(&mut self, visitor: &mut V) -> ControlFlow<V::Break>; | ||
| fn visit_mut<V: VisitorMut>(&mut self, visitor: &mut V) -> ControlFlow<V::Break>; | ||
| } | ||
|
|
||
| impl<T: Visit> Visit for Option<T> { | ||
|
|
@@ -87,26 +87,26 @@ impl<T: Visit> Visit for Box<T> { | |
| } | ||
|
|
||
| impl<T: VisitMut> VisitMut for Option<T> { | ||
| fn visit<V: VisitorMut>(&mut self, visitor: &mut V) -> ControlFlow<V::Break> { | ||
| fn visit_mut<V: VisitorMut>(&mut self, visitor: &mut V) -> ControlFlow<V::Break> { | ||
| if let Some(s) = self { | ||
| s.visit(visitor)?; | ||
| s.visit_mut(visitor)?; | ||
| } | ||
| ControlFlow::Continue(()) | ||
| } | ||
| } | ||
|
|
||
| impl<T: VisitMut> VisitMut for Vec<T> { | ||
| fn visit<V: VisitorMut>(&mut self, visitor: &mut V) -> ControlFlow<V::Break> { | ||
| fn visit_mut<V: VisitorMut>(&mut self, visitor: &mut V) -> ControlFlow<V::Break> { | ||
| for v in self { | ||
| v.visit(visitor)?; | ||
| v.visit_mut(visitor)?; | ||
| } | ||
| ControlFlow::Continue(()) | ||
| } | ||
| } | ||
|
|
||
| impl<T: VisitMut> VisitMut for Box<T> { | ||
| fn visit<V: VisitorMut>(&mut self, visitor: &mut V) -> ControlFlow<V::Break> { | ||
| T::visit(self, visitor) | ||
| fn visit_mut<V: VisitorMut>(&mut self, visitor: &mut V) -> ControlFlow<V::Break> { | ||
| T::visit_mut(self, visitor) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -118,7 +118,7 @@ macro_rules! visit_noop { | |
| } | ||
| })+ | ||
| $(impl VisitMut for $t { | ||
| fn visit<V: VisitorMut>(&mut self, _visitor: &mut V) -> ControlFlow<V::Break> { | ||
| fn visit_mut<V: VisitorMut>(&mut self, _visitor: &mut V) -> ControlFlow<V::Break> { | ||
| ControlFlow::Continue(()) | ||
| } | ||
| })+ | ||
|
|
@@ -320,7 +320,7 @@ pub trait Visitor { | |
| /// let mut statements = Parser::parse_sql(&GenericDialect{}, sql).unwrap(); | ||
| /// | ||
| /// // Drive the visitor through the AST | ||
| /// statements.visit(&mut Replacer); | ||
| /// statements.visit_mut(&mut Replacer); | ||
| /// | ||
| /// assert_eq!(statements[0].to_string(), "SELECT replaced FROM foo WHERE replaced IN (SELECT replaced FROM bar)"); | ||
| /// ``` | ||
|
|
@@ -503,7 +503,7 @@ where | |
| F: FnMut(&mut ObjectName) -> ControlFlow<E>, | ||
| { | ||
| let mut visitor = RelationVisitor(f); | ||
| v.visit(&mut visitor)?; | ||
| v.visit_mut(&mut visitor)?; | ||
| ControlFlow::Continue(()) | ||
| } | ||
|
|
||
|
|
@@ -633,7 +633,7 @@ where | |
| V: VisitMut, | ||
| F: FnMut(&mut Expr) -> ControlFlow<E>, | ||
| { | ||
| v.visit(&mut ExprVisitor(f))?; | ||
| v.visit_mut(&mut ExprVisitor(f))?; | ||
| ControlFlow::Continue(()) | ||
| } | ||
|
|
||
|
|
@@ -720,7 +720,7 @@ where | |
| V: VisitMut, | ||
| F: FnMut(&mut Statement) -> ControlFlow<E>, | ||
| { | ||
| v.visit(&mut StatementVisitor(f))?; | ||
| v.visit_mut(&mut StatementVisitor(f))?; | ||
| ControlFlow::Continue(()) | ||
| } | ||
|
|
||
|
|
@@ -1059,7 +1059,7 @@ mod tests { | |
|
|
||
| #[cfg(test)] | ||
| mod visit_mut_tests { | ||
| use crate::ast::{Ident, Statement, Value, ValueWithSpan, VisitMut, VisitorMut}; | ||
| use crate::ast::{Ident, Statement, Value, ValueWithSpan, Visit, VisitMut, Visitor, VisitorMut}; | ||
| use crate::dialect::GenericDialect; | ||
| use crate::parser::Parser; | ||
| use crate::tokenizer::Tokenizer; | ||
|
|
@@ -1092,7 +1092,7 @@ mod visit_mut_tests { | |
| .parse_statement() | ||
| .unwrap(); | ||
|
|
||
| let flow = s.visit(visitor); | ||
| let flow = s.visit_mut(visitor); | ||
| assert_eq!(flow, ControlFlow::Continue(())); | ||
| s | ||
| } | ||
|
|
@@ -1139,4 +1139,26 @@ mod visit_mut_tests { | |
| let mutated = do_visit_mut("SELECT a, b FROM t", &mut visitor); | ||
| assert_eq!(mutated.to_string(), "SELECT A, B FROM T"); | ||
| } | ||
|
|
||
| struct DummyVisitor; | ||
| impl Visitor for DummyVisitor { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest you have the dummy visitor do something, like count nodes |
||
| type Break = (); | ||
| } | ||
|
|
||
| struct DummyVisitorMut; | ||
| impl VisitorMut for DummyVisitorMut { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And the dummy visitor mut to edit the nodes it visits. |
||
| type Break = (); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_both_visit_and_visit_mut() { | ||
| let mut visitor = DummyVisitor; | ||
| let mut visitor_mut = DummyVisitorMut; | ||
| let mut statements = Parser::parse_sql(&GenericDialect {}, "SELECT 1").unwrap(); | ||
|
|
||
| let _ = statements.visit(&mut visitor); | ||
| let _ = statements.visit_mut(&mut visitor_mut); | ||
|
|
||
| assert_eq!(statements[0].to_string(), "SELECT 1"); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just eyeballing, but I would wager you need to run a
cargo fmt --all -- --check.