Skip to content

Commit 4224cf5

Browse files
authored
[record_use] Canonicalize before serialization (dart-lang#3155)
1 parent 3af7688 commit 4224cf5

8 files changed

Lines changed: 419 additions & 171 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'constant.dart';
6+
import 'definition.dart';
7+
import 'loading_unit.dart';
8+
9+
/// A context used to canonicalize [Definition]s, [LoadingUnit]s, and
10+
/// [MaybeConstant]s.
11+
class CanonicalizationContext {
12+
final Set<Definition> _definitions = {};
13+
final Set<LoadingUnit> _loadingUnits = {};
14+
final Set<MaybeConstant> _constants = {};
15+
16+
/// Canonicalizes the given [Definition].
17+
Definition canonicalizeDefinition(Definition definition) {
18+
final existing = _definitions.lookup(definition);
19+
if (existing != null) return existing;
20+
final canonical = definition.canonicalizeChildren(this);
21+
_definitions.add(canonical);
22+
return canonical;
23+
}
24+
25+
/// Canonicalizes the given [LoadingUnit].
26+
LoadingUnit canonicalizeLoadingUnit(LoadingUnit loadingUnit) {
27+
final existing = _loadingUnits.lookup(loadingUnit);
28+
if (existing != null) return existing;
29+
final canonical = loadingUnit.canonicalizeChildren(this);
30+
_loadingUnits.add(canonical);
31+
return canonical;
32+
}
33+
34+
/// Canonicalizes the given [MaybeConstant].
35+
MaybeConstant canonicalizeConstant(MaybeConstant constant) {
36+
final existing = _constants.lookup(constant);
37+
if (existing != null) return existing;
38+
final canonical = constant.canonicalizeChildren(this);
39+
_constants.add(canonical);
40+
return canonical;
41+
}
42+
43+
/// All canonicalized [Definition]s.
44+
Iterable<Definition> get definitions => _definitions;
45+
46+
/// All canonicalized [LoadingUnit]s.
47+
Iterable<LoadingUnit> get loadingUnits => _loadingUnits;
48+
49+
/// All canonicalized [MaybeConstant]s.
50+
Iterable<MaybeConstant> get constants => _constants;
51+
}

‎pkgs/record_use/lib/src/constant.dart‎

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'dart:math';
66

77
import 'package:meta/meta.dart';
88

9+
import 'canonicalization_context.dart';
910
import 'definition.dart';
1011
import 'helper.dart';
1112
import 'serialization_context.dart';
@@ -27,6 +28,9 @@ sealed class MaybeConstant {
2728
/// `hashCode` because it is implicitly covered by the content-based hash.
2829
int get _size;
2930

31+
/// Canonicalizes this [MaybeConstant].
32+
MaybeConstant _canonicalizeChildren(CanonicalizationContext context);
33+
3034
/// Compares this [MaybeConstant] with [other] for semantic equality.
3135
///
3236
/// If [allowPromotionOfUnsupported] is true, an [UnsupportedConstant] in
@@ -171,6 +175,9 @@ final class NonConstant extends MaybeConstant {
171175
@override
172176
int get _size => 1;
173177

178+
@override
179+
MaybeConstant _canonicalizeChildren(CanonicalizationContext context) => this;
180+
174181
@override
175182
int get hashCode => 0x4e6f6e43;
176183

@@ -196,6 +203,9 @@ abstract class Constant extends MaybeConstant {
196203
/// Creates a [Constant] object.
197204
const Constant();
198205

206+
@override
207+
Constant _canonicalizeChildren(CanonicalizationContext context);
208+
199209
@override
200210
@visibleForTesting
201211
bool semanticEquals(
@@ -233,6 +243,9 @@ final class NullConstant extends Constant {
233243
@override
234244
int get _size => 1;
235245

246+
@override
247+
Constant _canonicalizeChildren(CanonicalizationContext context) => this;
248+
236249
@override
237250
int get hashCode => 0x4e756c6c;
238251

@@ -268,6 +281,9 @@ final class UnsupportedConstant extends Constant {
268281
@override
269282
int get _size => 1;
270283

284+
@override
285+
Constant _canonicalizeChildren(CanonicalizationContext context) => this;
286+
271287
@override
272288
int get hashCode => message.hashCode;
273289

@@ -303,6 +319,9 @@ final class BoolConstant extends Constant {
303319
@override
304320
int get _size => 1;
305321

322+
@override
323+
Constant _canonicalizeChildren(CanonicalizationContext context) => this;
324+
306325
@override
307326
bool operator ==(Object other) =>
308327
other is BoolConstant && other.value == value;
@@ -338,6 +357,9 @@ final class IntConstant extends Constant {
338357
@override
339358
int get _size => 1;
340359

360+
@override
361+
Constant _canonicalizeChildren(CanonicalizationContext context) => this;
362+
341363
@override
342364
bool operator ==(Object other) =>
343365
other is IntConstant && other.value == value;
@@ -373,6 +395,9 @@ final class StringConstant extends Constant {
373395
@override
374396
int get _size => 1;
375397

398+
@override
399+
Constant _canonicalizeChildren(CanonicalizationContext context) => this;
400+
376401
@override
377402
bool operator ==(Object other) =>
378403
other is StringConstant && other.value == value;
@@ -413,6 +438,9 @@ final class SymbolConstant extends Constant {
413438
@override
414439
int get _size => 1;
415440

441+
@override
442+
Constant _canonicalizeChildren(CanonicalizationContext context) => this;
443+
416444
@override
417445
bool operator ==(Object other) =>
418446
other is SymbolConstant &&
@@ -466,6 +494,12 @@ final class ListConstant extends Constant {
466494
return 1 + size;
467495
});
468496

497+
@override
498+
Constant _canonicalizeChildren(CanonicalizationContext context) =>
499+
ListConstant(
500+
value.map((c) => context.canonicalizeConstant(c) as Constant).toList(),
501+
);
502+
469503
@override
470504
bool operator ==(Object other) {
471505
if (identical(this, other)) return true;
@@ -537,6 +571,19 @@ final class MapConstant extends Constant {
537571
return 1 + size;
538572
});
539573

574+
@override
575+
Constant _canonicalizeChildren(CanonicalizationContext context) =>
576+
MapConstant(
577+
entries
578+
.map(
579+
(e) => MapEntry(
580+
context.canonicalizeConstant(e.key) as Constant,
581+
context.canonicalizeConstant(e.value) as Constant,
582+
),
583+
)
584+
.toList(),
585+
);
586+
540587
@override
541588
bool operator ==(Object other) {
542589
if (identical(this, other)) return true;
@@ -655,6 +702,20 @@ final class InstanceConstant extends Constant {
655702
return 1 + size;
656703
});
657704

705+
@override
706+
Constant _canonicalizeChildren(CanonicalizationContext context) =>
707+
InstanceConstant(
708+
definition: context.canonicalizeDefinition(definition),
709+
fields: Map.fromEntries(
710+
fields.entries.map(
711+
(e) => MapEntry(
712+
e.key,
713+
context.canonicalizeConstant(e.value) as Constant,
714+
),
715+
),
716+
),
717+
);
718+
658719
@override
659720
String toString() =>
660721
'InstanceConstant($definition, {'
@@ -757,6 +818,22 @@ final class EnumConstant extends Constant {
757818
return 1 + size;
758819
});
759820

821+
@override
822+
Constant _canonicalizeChildren(CanonicalizationContext context) =>
823+
EnumConstant(
824+
definition: context.canonicalizeDefinition(definition),
825+
index: index,
826+
name: name,
827+
fields: Map.fromEntries(
828+
fields.entries.map(
829+
(e) => MapEntry(
830+
e.key,
831+
context.canonicalizeConstant(e.value) as Constant,
832+
),
833+
),
834+
),
835+
);
836+
760837
@override
761838
String toString() =>
762839
'EnumConstant($definition, index: $index, name: $name, fields: {'
@@ -852,6 +929,22 @@ final class RecordConstant extends Constant {
852929
return 1 + size;
853930
});
854931

932+
@override
933+
Constant _canonicalizeChildren(CanonicalizationContext context) =>
934+
RecordConstant(
935+
positional: positional
936+
.map((c) => context.canonicalizeConstant(c) as Constant)
937+
.toList(),
938+
named: Map.fromEntries(
939+
named.entries.map(
940+
(e) => MapEntry(
941+
e.key,
942+
context.canonicalizeConstant(e.value) as Constant,
943+
),
944+
),
945+
),
946+
);
947+
855948
@override
856949
String toString() =>
857950
'RecordConstant('
@@ -896,6 +989,9 @@ final class RecordConstant extends Constant {
896989
extension MaybeConstantProtected on MaybeConstant {
897990
ConstantSyntax toSyntax(SerializationContext context) => _toSyntax(context);
898991

992+
MaybeConstant canonicalizeChildren(CanonicalizationContext context) =>
993+
_canonicalizeChildren(context);
994+
899995
static MaybeConstant fromSyntax(
900996
ConstantSyntax syntax,
901997
DeserializationContext context,

‎pkgs/record_use/lib/src/definition.dart‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'package:meta/meta.dart';
66

7+
import 'canonicalization_context.dart';
78
import 'helper.dart';
89
import 'syntax.g.dart';
910

@@ -66,6 +67,9 @@ class Definition {
6667
.toList(),
6768
);
6869

70+
/// Canonicalizes the children of this [Definition].
71+
Definition _canonicalizeChildren(CanonicalizationContext context) => this;
72+
6973
/// The parent, if it exists.
7074
Definition? get parent => path.length > 1
7175
? Definition(library, path.sublist(0, path.length - 1))
@@ -273,6 +277,9 @@ final class DefinitionDisambiguator {
273277
extension DefinitionProtected on Definition {
274278
DefinitionSyntax toSyntax() => _toSyntax();
275279

280+
Definition canonicalizeChildren(CanonicalizationContext context) =>
281+
_canonicalizeChildren(context);
282+
276283
static Definition fromSyntax(DefinitionSyntax syntax) =>
277284
Definition._fromSyntax(syntax);
278285
}

‎pkgs/record_use/lib/src/loading_unit.dart‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'canonicalization_context.dart';
56
import 'definition.dart';
67

78
/// A loading unit in which a usage of a [Definition] was recorded.
@@ -11,6 +12,9 @@ final class LoadingUnit {
1112

1213
const LoadingUnit(this.name);
1314

15+
/// Canonicalizes the children of this [LoadingUnit].
16+
LoadingUnit _canonicalizeChildren(CanonicalizationContext context) => this;
17+
1418
@override
1519
bool operator ==(Object other) {
1620
if (identical(this, other)) return true;
@@ -24,3 +28,12 @@ final class LoadingUnit {
2428
@override
2529
String toString() => 'LoadingUnit($name)';
2630
}
31+
32+
/// Package private (protected) methods for [LoadingUnit].
33+
///
34+
/// This avoids bloating the public API and public API docs and prevents
35+
/// internal types from leaking from the API.
36+
extension LoadingUnitProtected on LoadingUnit {
37+
LoadingUnit canonicalizeChildren(CanonicalizationContext context) =>
38+
_canonicalizeChildren(context);
39+
}

0 commit comments

Comments
 (0)