Skip to content

Remove unnecessary guard in TreeMap #18368

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

Merged
Merged
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
3 changes: 3 additions & 0 deletions compiler/src/dotty/tools/dotc/ast/Trees.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1596,6 +1596,9 @@ object Trees {
case tree @ TypeDef(name, rhs) =>
cpy.TypeDef(tree)(name, transform(rhs))
case tree @ Template(constr, parents, self, _) if tree.derived.isEmpty =>
Copy link
Contributor

Choose a reason for hiding this comment

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

The guard made sure nothing was forgotten in the transform. Instead of a guard it could be an assert. But then we could not fill in the missing case (where a derived is not empty) in transformMoreCases.

So I think overall it's best to leave it in. Maybe add a comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did test it locally with an assert and then removed it. I could add back the assert with a meaningful message. It might be clearer this way that the Template was not supposed to have a derived set.

Are we only supposed to have non-empty derived for untyped templates? Or are there cases where the typed template is expected to have something in derived?

Copy link
Contributor

Choose a reason for hiding this comment

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

We don't know. As you tested, there are no occurrences where derived is empty when we transform. But that is not necessarily true for the future. With the original scheme we fall through into transformMoreCases when we see such a tree where we would fail unless transformMoreCases explicitly provides for it. I think that's what we want. An assert is worse since it prevents the handling of the case. The chance is high that if such a case arises in the future, and we hit the assert, we just remove it, since we have now found a legitimate case where it mis-fires. But that would be bad since then other maps would have a hole where they silently drop the derived field. So I really think the original code is the best possible design.

// Currently we do not have cases where we expect `tree.derived` to contain trees for typed trees.
// If it is the case we will fall in `transformMoreCases` and throw an exception there.
// In the future we might keep the `derived` clause after typing, in that case we might want to start handling it here.
cpy.Template(tree)(transformSub(constr), transform(tree.parents), Nil, transformSub(self), transformStats(tree.body, tree.symbol))
case Import(expr, selectors) =>
cpy.Import(tree)(transform(expr), selectors)
Expand Down