refactor(difc): eliminate SecrecyLabel/IntegrityLabel structural duplication via Go generics#5545
Conversation
…Label[T]
Eliminates ~34 lines of structural duplication by introducing a single
generic FlowLabel[T any] type parameterized by phantom kinds secrecyKind
and integrityKind. The four duplicate method pairs (getLabel, CanFlowTo,
CheckFlow, Clone) are now expressed once on FlowLabel[T].
Two thin type aliases preserve the public API unchanged:
type SecrecyLabel = FlowLabel[secrecyKind]
type IntegrityLabel = FlowLabel[integrityKind]
Two package-private helper functions (flowIsSubset[T], flowTypeName[T])
derive the per-kind behaviour (subset vs superset semantics, log label)
from the type parameter at compile time, so no extra fields are needed
and direct struct literals such as &SecrecyLabel{Label: nil} continue to
work correctly.
All existing tests pass without modification.
…sertions Replace the flowIsSubset[T]/flowTypeName[T] free functions (which used runtime type assertions) with a labelKind interface constraint that the phantom kind types implement directly. The compiler can now statically dispatch isSubset() and typeName() calls without any type switch overhead. No behaviour changes; all tests continue to pass.
There was a problem hiding this comment.
Pull request overview
Refactors DIFC label types to remove duplicated logic between SecrecyLabel and IntegrityLabel by introducing a single generic FlowLabel[T] parameterized by a phantom “kind” type that encodes flow semantics.
Changes:
- Introduces
labelKindplussecrecyKind/integrityKindphantom types to encode subset/superset flow rules. - Replaces the two concrete label structs with
typealiases toFlowLabel[...], preserving existing call sites. - Consolidates duplicated methods (
getLabel,CanFlowTo,CheckFlow,Clone) into the genericFlowLabel[T]implementation.
Show a summary per file
| File | Description |
|---|---|
internal/difc/labels.go |
Replaces duplicated Secrecy/Integrity label implementations with a generic FlowLabel[T] plus kind-specific semantics. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 1/1 changed files
- Comments generated: 1
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
|
@copilot fix the failing ci check |
The previous commit renamed the generic struct type from FlowLabel to flowLabel but left the method receivers and return types still using the old name, causing build failures. Update all receiver and return type references to use flowLabel[T].
The CI failure was caused by an incomplete rename in commit |
✨ Enhancement
SecrecyLabelandIntegrityLabelininternal/difc/labels.gowere structurally identical types with 6 mirrored method pairs (~34 duplicated lines), diverging only in thecheckSubsetboolean passed tocheckFlowHelper. Any new method added to one type had to be manually mirrored.What does this improve?
Replaces both concrete structs with a single
FlowLabel[T labelKind]generic type. The four duplicate method bodies (getLabel,CanFlowTo,CheckFlow,Clone) are now defined once.Implementation approach:
labelKindinterface constraint — phantom kind types encode their flow semantics directly via interface methods, avoiding runtime type assertions in the hot path:FlowLabel[T labelKind]— single generic struct with shared methods;var kind Tin each method body dispatches statically to the kind's implementation:Type aliases preserve the public API unchanged — no call sites require modification:
Direct struct literals like
&SecrecyLabel{Label: nil}continue to compile correctly sinceFlowLabelcarries no extra fields.