Skip to content

Counted by typeloc downstream#12925

Open
hnrklssn wants to merge 2 commits intoswiftlang:nextfrom
hnrklssn:counted-by-typeloc-downstream
Open

Counted by typeloc downstream#12925
hnrklssn wants to merge 2 commits intoswiftlang:nextfrom
hnrklssn:counted-by-typeloc-downstream

Conversation

@hnrklssn
Copy link
Copy Markdown
Member

@hnrklssn hnrklssn commented May 8, 2026

This cherry-picks llvm#167287 and makes adjustments to remove the current lexer-based approach for finding source ranges.

hnrklssn added 2 commits May 7, 2026 23:19
Cherry-pick of llvm#167287 adapted
to the downstream fork.

Populate BoundsAttributedTypeLoc with the attribute's source range so
that diagnostics for incomplete pointee types can emit accurate fix-it
hints (e.g. suggesting __sized_by instead of __counted_by). Previously
the TypeLoc carried no data and source ranges were approximated from the
count expression.

Key changes:
- BoundsAttributedLocInfo now stores a SourceRange
- BoundsAttributedTypeLoc gains setAttrRange/getAttrRange,
  getAttrNameAsWritten, getAttrNameRange, and getLocalSourceRange
- handleCountedByAttrField builds proper TypeSourceInfo via TypeLocBuilder
- TransformCountAttributedType propagates attrRange
- EmitIncompleteCountedByPointeeNotes uses TypeLoc for fix-its, with
  SourceRangeFor as fallback for BoundsSafety-specific callers
- TypeLocFinder visitor extracts TypeLoc from expressions

Additional fixes beyond the upstream PR:
- Null safety in getTSI() and TypeLocFinder visitor methods
- getLocalSourceRange() on BoundsAttributedTypeLoc to prevent infinite
  recursion through TypeLocRanger
…stics

Replace the heuristic SourceRangeFor() implementation which reverse-
engineered attribute source ranges by scanning backwards from the count
expression with the Lexer, with a TypeLoc-based approach now that
counted_by has a TypeLoc.

To help with this we introduce Sema::TypeLocSource, a small wrapper with
factory methods (fromAssignee, fromParameter, fromExpression,
fromReturnType) that resolve the appropriate TypeLoc for each context.
This lets BoundsSafetyCheckAssignmentToCountAttrPtr receive a
pre-resolved TypeLoc rather than containing a bunch of special cased
logic to extract TypeLocs for various scenarios.

Additional changes:
- Add VisitImplicitCastExpr, VisitUnaryDeref, VisitArraySubscriptExpr
  to TypeLocFinder so that subscript and dereference expressions resolve
  to the underlying declaration's TypeLoc
- Extract getCountAttributedTypeLoc() helper for walking through pointer
  and function-return TypeLoc layers
- Remove SourceRangeFor() and its ~100 lines of fragile Lexer scanning
- Source locations now point to the attribute token start rather than
  the count expression position

The SourceRangeFor fallback is removed because all known call paths now
provide a TypeLoc via TypeLocSource. When TypeLoc resolution fails (e.g.
unhandled expression types), the "consider using __sized_by" note is
silently suppressed rather than emitted at an approximate location.
@hnrklssn
Copy link
Copy Markdown
Member Author

hnrklssn commented May 8, 2026

@swift-ci please test llvm

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant