Skip to content

Ship generic-attribute syntax once Unity bundles Roslyn 4.4+ #2

@svermeulen

Description

@svermeulen

The source generator already reads AttributeClass.TypeArguments (committed in b7ada7a5d81), so the moment the Trecs core package can declare SingleEntityAttribute<T1..T4> / ForEachEntityAttribute<T1..T4> / FromWorldAttribute<T1..T4>, users get the C# 11 generic-attribute shorthand:

[SingleEntity<TuskTags.Player>]            // vs. [SingleEntity(typeof(TuskTags.Player))]
[ForEachEntity<TuskTags.Fish, Eating>]     // vs. [ForEachEntity(typeof(Fish), typeof(Eating))]
[FromWorld<TuskTags.Fish>]

Why it isn't shipped today

Unity 6's bundled Roslyn predates 4.4, so generic attributes are stuck behind -langversion:preview. We tested both paths:

  • -langversion:11 — rejected outright: CS1617 Invalid option '11' for /langversion.
  • -langversion:preview — accepts generic attributes, but simultaneously activates stricter C# 11 ref-safety analysis. That surfaces ~10 pre-existing CS8347 / CS8168 errors in NativeWorldAccessor.cs and EntitySubmitter.cs (Cannot return local 'bag' by reference, may expose variables referenced by parameter), which is a real refactor outside the scope of this feature.

What's needed when we revisit

  1. Confirm Unity ships Roslyn 4.4+ (or fix the ref-safety regressions to opt into preview cleanly).
  2. Add csc.rsp with -langversion:latest (or :11) at Packages/com.trecs.core/Scripts/.
  3. Re-add the four-arity generic attribute classes to SingleEntityAttribute.cs, ForEachEntityAttribute.cs, FromWorldAttribute.cs — capped at 4 to match WithTags<T1, T2, T3, T4>. ForEachEntity's generic variants need Set / MatchByComponents properties; the others are bare.
  4. No source-gen changes required — TypeArguments parsing is already wired through InlineTagsParser, IterationCriteriaParser, the FromWorld paths in JobGenerator/AutoJobGenerator, and AutoSystemGenerator.HasAnyAttributeCriteria. The conflict diagnostic (TRECS053) already covers [X<T>] mixed with named Tag / Tags.
  5. Tests are already in place — RunOnceGeneratorTests.RunOnce_GenericAttribute* exercise the path via stubs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions