typeToString can now show (recursively) resolved type aliases; fixes #8569 #8083 #8570 #11678
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
echo A[B]prints differently depending on unrelated code #8569typetraits.namebehaves unexpected #8083CString=>cstringandChar=>char,openarray=>openArray(more consistent with all other types, seetypeToStr)(also related to [typetraits] Tuple type name confusion #7976)
details
typeToString gets 2 new options (and typeToString gets exposed as a magic), which in future PRs could replace all existing
TPreferedDescoptions:preferResolvedto show (recursively) resolved type aliases (tuple[a: MyInt]shows astuple[a: int]) (note thatpreferDescdoesn't do that reliably)preferMixedto show (recursively) resolved type aliases + their aliases when they differ (at the place where they differ): (tuple[a: MyInt]shows astuple[a: MyInt{int}])this fixes above mentioned bugs and also makes it clear to user what a type resolves to, while also showing original type if using
preferMixed(egcint), in a way more compact than current (unreliable) way of showing bothpreferName+preferDesc; it helps notably in type mismatch/sigmatch errors.See unittest in tests/metatype/ttypetraits2.nim for detailed behavior; here's a snippet:
Existing code is unaffected except for the fixed typename case (such as
CString=>cstring)in future PR we can use the
preferMixed(eg to replacepreferDescand expose it in typetraits.typeToString)I kept
proc typeToString*(t: typedesc, prefer = "preferTypeName"): string {.magic: "TypeTrait".}out of typetraits.nim for now until we're sure of API; as for the public name for the new proc, usingtypeToStringas I did instead of overloadingnameseems like a good choice (named just like the proc in compiler/types.nim), becausenameis such a common word it keeps causing issues (as noted by @kaushalmodi here #7975 (comment))type outType = type(10)prints asint literal(10)instead ofint#8704 by printingint literal(10)instead ofintwith the optionpreferResolvedpreferDesc(in which case i can just remove the newly added preferResolved and make preferDesc use its implementation); but I'd rather do this in a separate PRimplementation notes
maybe easier to view the diff in a terminal with
git diff -wto view the diff without getting confused by re-indentation; the 1st commit doesn't change semantics but just re-indentstypeToString(hence seemingly large diff), adding a nestedtypeToString(typ: PType)inside top-levelproc typeToString(typ: PType, prefer: TPreferedDesc = preferName): string; this makes implementation much easier.the remaining commits do the actual semantic changes
note:
CI failure seems unrelated (caused by #11708)