From 0e86c0352634acafc4e828e26b16ecabfa66aba1 Mon Sep 17 00:00:00 2001 From: Liam Appelbe Date: Thu, 13 Nov 2025 13:46:20 +1100 Subject: [PATCH 1/3] WIP: testing tighter scoping rules --- pkgs/ffigen/lib/src/code_generator/scope.dart | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pkgs/ffigen/lib/src/code_generator/scope.dart b/pkgs/ffigen/lib/src/code_generator/scope.dart index d3266771d..f107c2ae1 100644 --- a/pkgs/ffigen/lib/src/code_generator/scope.dart +++ b/pkgs/ffigen/lib/src/code_generator/scope.dart @@ -48,7 +48,10 @@ class Scope { /// [fillNames] must not have been called yet. void add(Symbol? symbol) { assert(!_filled); - if (symbol != null) _symbols.add(symbol); + if (symbol != null && symbol._scope == null) { + _symbols.add(symbol); + symbol.scope = this; + } } /// Add an ad-hoc name to the [Scope]. @@ -161,6 +164,7 @@ class Namer { class Symbol extends AstNode { final String oldName; final SymbolKind kind; + Scope? _scope; String? _name; /// Only valid if [Scope.fillNames] has been called already. From c78ca49afd303688284c1ce859196caf17e4cd25 Mon Sep 17 00:00:00 2001 From: Liam Appelbe Date: Thu, 13 Nov 2025 14:22:46 +1100 Subject: [PATCH 2/3] fix analysis --- pkgs/ffigen/lib/src/code_generator/scope.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/ffigen/lib/src/code_generator/scope.dart b/pkgs/ffigen/lib/src/code_generator/scope.dart index f107c2ae1..cf0af305f 100644 --- a/pkgs/ffigen/lib/src/code_generator/scope.dart +++ b/pkgs/ffigen/lib/src/code_generator/scope.dart @@ -50,7 +50,7 @@ class Scope { assert(!_filled); if (symbol != null && symbol._scope == null) { _symbols.add(symbol); - symbol.scope = this; + symbol._scope = this; } } From 38877d1eaf71cc94b675cf8deffaf9eacb07ca6e Mon Sep 17 00:00:00 2001 From: Liam Appelbe Date: Fri, 14 Nov 2025 11:08:39 +1100 Subject: [PATCH 3/3] Fix test --- pkgs/ffigen/lib/src/code_generator/scope.dart | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/pkgs/ffigen/lib/src/code_generator/scope.dart b/pkgs/ffigen/lib/src/code_generator/scope.dart index cf0af305f..ae44fb79f 100644 --- a/pkgs/ffigen/lib/src/code_generator/scope.dart +++ b/pkgs/ffigen/lib/src/code_generator/scope.dart @@ -40,20 +40,28 @@ class Scope { /// Add a [Symbol] to this [Scope]. /// - /// It's fine to add the [Symbol] to this [Scope] multiple times. It's - /// also fine to add the [Symbol] to multiple [Scope]s, as long as one of - /// the [Scope]s is an ancestor of all the others (this is checked during - /// [fillNames]). + /// [Symbol]s can only be in one [Scope], so this call does nothing if the + /// symbol is already in a scope, unless this scope is an ancestor of the + /// symbol's existing scope. /// /// [fillNames] must not have been called yet. void add(Symbol? symbol) { assert(!_filled); - if (symbol != null && symbol._scope == null) { + if (symbol == null) return; + final existingScope = symbol._scope; + if (existingScope == null || _isAncestor(existingScope)) { _symbols.add(symbol); symbol._scope = this; } } + bool _isAncestor(Scope other) { + for (Scope? s = other; s != null; s = s._parent) { + if (s == this) return true; + } + return false; + } + /// Add an ad-hoc name to the [Scope]. /// /// This is meant to be used during code generation, for generating unique