Skip to content

Conversation

liamappelbe
Copy link
Contributor

@liamappelbe liamappelbe commented Oct 3, 2025

Some of the transformation passes are built under the assumption that supertypes are 100% done processing before child types start processing. To ensure this ordering, they would visit the current node's supertype, then do the current node's processing, then visit the current node's children.

This usually worked, but there are APIs where this approach didn't work:

class A {
  void m(C c);
}

class B extends A {}

class C extends B {}

If the visitor happens to begin its traversal at B, then the iteration looks like (B (A (C) A) B), because it follows A's method m to C before B is done processing. When C tries to iterate to its super type, the visitor logic returns immediately, since B is already being visited, so C begins its processing before B is finished.

This approach would work if the AST was a DAG, but if you include full transitive iteration over all the class's methods, then it's no longer a DAG. So to fix the bug, I've added an option to the visitChildren method that only iterates the DAGy parts of the ObjC type graph.

Any visitor that cares about this strict ordering of supertypes before subtypes should set typeGraphOnly: true when iterating over interfaces, protocols, and categories. This was a straightforward change in CopyMethodsFromSuperTypesVisitation, and FixOverriddenMethodsVisitation.

But when I tried to apply it in FindSymbolsVisitation, it was more complicated. The main thing typeGraphOnly: true does is stop iterating over the methods, but FindSymbolsVisitation needs to iterate over the methods. I found it was easier to split this visitor into two, one that creates the scopes, and one that adds symbols to scopes. But CreateScopesVisitation still needs to visit the methods to create their scopes, so this visitor is run twice, once with typeGraphOnly: true to set the scope parenting correctly, and once with typeGraphOnly: false to hit the AST nodes that were missed by the first pass.

The other significant change is that ObjCMethod's local scope is now parented to the super-most type that has the method, instead of sitting in the global scope. This causes some annoying naming decisions (eg some NSData methods that take a length param now call it length$1 to avoid colliding with the length method). But these will be fixed by #2054

Fixes #2656

Copy link

github-actions bot commented Oct 3, 2025

PR Health

Breaking changes ✔️
Package Change Current Version New Version Needed Version Looking good?
objective_c Breaking 8.1.0 9.0.0-dev.0 9.0.0 ✔️

This check can be disabled by tagging the PR with skip-breaking-check.

API leaks ⚠️

The following packages contain symbols visible in the public API, but not exported by the library. Export these symbols or remove them from your publicly visible API.

Package Leaked API symbol Leaking sources
objective_c _FinalizablePointer internal.dart::_ObjCReference::new::_finalizable

This check can be disabled by tagging the PR with skip-leaking-check.

License Headers ⚠️
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
Files
pkgs/objective_c/lib/src/ns_input_stream.dart

All source files should start with a license header.

This check can be disabled by tagging the PR with skip-license-check.

@liamappelbe liamappelbe changed the title [ffigen] Fix some subtle AST iteration order bugs WIP [ffigen] Fix some subtle AST iteration order bugs Oct 3, 2025
@coveralls
Copy link

coveralls commented Oct 3, 2025

Coverage Status

coverage: 80.854% (+0.05%) from 80.804%
when pulling 2c9793b on ffigen_ast_ordering
into 0924cb0 on main.

@liamappelbe liamappelbe changed the title WIP [ffigen] Fix some subtle AST iteration order bugs [ffigen] Fix some subtle AST iteration order bugs Oct 6, 2025
@liamappelbe liamappelbe marked this pull request as ready for review October 6, 2025 23:02
@HosseinYousefi
Copy link
Member

This might also be a problem for jnigen. I need to investigate!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[ffigen][objective_c] Adding header for superclass causes the constructor to not be generated
3 participants