This repository was archived by the owner on Jun 19, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Clean up string conversion semantics #54
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2115,6 +2115,13 @@ func (c *checker) checkTypeParamConstraintsAt(loc source.Location, params []*typ | |
| c.reportTypeMismatch(loc, constraint, actual) | ||
| continue | ||
| } | ||
| if _, ok := constraint.(*typeinfo.StringableConstraint); ok { | ||
| if c.isStringableType(actual) { | ||
| continue | ||
| } | ||
| c.reportTypeMismatch(loc, constraint, actual) | ||
| continue | ||
| } | ||
| if c.assignable(constraint, actual) { | ||
| continue | ||
| } | ||
|
|
@@ -2126,6 +2133,17 @@ func (c *checker) checkTypeParamConstraintsAt(loc source.Location, params []*typ | |
| } | ||
| } | ||
|
|
||
| func (c *checker) isStringableType(typ typeinfo.Type) bool { | ||
| base := c.underlying(typ) | ||
| if approx, ok := base.(*typeinfo.ApproxType); ok && approx != nil { | ||
| return c.isStringableType(approx.Inner) | ||
| } | ||
| return c.isStringType(typ) || | ||
| typeinfo.IsNumeric(base) || | ||
| c.isCharSliceType(typ) || | ||
| c.isStringMethodCoercion(&typeinfo.StringType{}, typ) | ||
| } | ||
|
Comment on lines
+2136
to
+2145
|
||
|
|
||
| func (c *checker) checkInstantiatedGenericRequirements(call *ast.CallExpr, callee ast.Node, bindings map[*typeinfo.TypeParam]typeinfo.Type) bool { | ||
| sym := c.resolvedFunctionSymbol(callee) | ||
| if sym == nil { | ||
|
|
@@ -3070,14 +3088,16 @@ func (c *checker) isExplicitEnumCast(target, source typeinfo.Type) bool { | |
| } | ||
|
|
||
| func (c *checker) isExplicitStringCast(target, source typeinfo.Type) bool { | ||
| if c.isStringType(target) && (c.isByteSliceType(source) || c.isCharSliceType(source) || typeinfo.IsNumeric(source)) { | ||
| return true | ||
| } | ||
| if c.isStringMethodCoercion(target, source) { | ||
| return true | ||
| } | ||
| if c.isStringType(source) && (c.isByteSliceType(target) || c.isCharSliceType(target)) { | ||
| return true | ||
| if c.isStringType(source) { | ||
| targetSlice, ok := c.underlying(target).(*typeinfo.SliceType) | ||
| return ok && targetSlice != nil && !targetSlice.Mutable && typeinfo.IsBuiltinNamed(targetSlice.Inner, "u8") | ||
| } | ||
| if c.isStringType(target) { | ||
| sourceSlice, ok := c.underlying(source).(*typeinfo.SliceType) | ||
| return ok && sourceSlice != nil && !sourceSlice.Mutable && typeinfo.IsBuiltinNamed(sourceSlice.Inner, "u8") | ||
| } | ||
| return false | ||
| } | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
to_strdocs here recommend usingasfor zero-copy reshapes, but don’t mention that[]u8 as stris a trusted view that does not validate UTF-8 (per CURRENT_SEMANTICS). Consider adding an explicit warning in this comment block so callers don’t assume the cast is validating/safe for arbitrary bytes.