Skip to content

[SR-11889] Use a Located<T> throughout the compiler instead of std::pair<SourceLoc, T> #54305

@typesanitizer

Description

@typesanitizer
Previous ID SR-11889
Radar rdar://problem/57579815
Original Reporter @typesanitizer
Type Improvement
Status Closed
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Improvement, StarterBug
Assignee mkita (JIRA)
Priority Medium

md5: d65bfa4b042fd60690d622e6c0308dd8

Issue Description:

Using std::pair<SourceLoc, T> or std::pair<T, SourceLoc> for fields makes reading usage sites harder as one needs to keep track of the types of the first and second field. It would be good to have a currency type:

template<typename T>
struct Located<T> {
  SourceLoc loc;
  T item;
}

and use that consistently throughout the compiler. For example,

  typedef ArrayRef<std::pair<Identifier, SourceLoc>> AccessPathTy;

could be replaced with

  typedef ArrayRef<Located<Identifier>> AccessPathTy;

Q: Where should Located<T> be defined?
A: I think it should be defined in a new header, Basic/Located.h. Basic because we expect it to be used throughout the compiler, similar to SourceLoc.

Q: What implicit conversions should Located<T> have?
A: We probably don't want an implicit conversion from T because that makes it easier for someone to create a Located<T> that conceptually doesn't make sense. Similarly we probably don't want a conversion from Located<T> to T as that makes it easy to accidentally drop a SourceLoc.

Q: Which operators should be overloaded?
A: Probably just the shift operator to make things convenient for print debugging. We can add more stuff later if the usage sites make it seem like more operator overloads would be useful.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions