-
Notifications
You must be signed in to change notification settings - Fork 523
Improved sprintf() inference #3232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
9b5a686
8b93038
fd5b5c9
b3cfcb3
33c415d
93ce92a
83b20de
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,10 @@ function returnsJustString(): string | |
return rand(0,1) === 1 ? 'foo' : ''; | ||
} | ||
|
||
function returnsBool(): bool { | ||
return true; | ||
} | ||
|
||
$s = sprintf("%s", returnsNonEmptyString()); | ||
assertType('non-empty-string', $s); | ||
|
||
|
@@ -41,3 +45,12 @@ function returnsJustString(): string | |
|
||
$s = sprintf('%2$s', 1234, returnsNonFalsyString()); | ||
assertType('non-falsy-string', $s); | ||
|
||
$s = sprintf('%20s', 'abc'); | ||
assertType("' abc'", $s); | ||
|
||
$s = sprintf('%20s', true); | ||
assertType("' 1'", $s); | ||
|
||
$s = sprintf('%20s', returnsBool()); | ||
assertType("non-falsy-string", $s); | ||
Comment on lines
+49
to
+56
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. regressions from #3167 which made There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. //cc @pilif |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ public function inputTypes(int $i, float $f, string $s) { | |
|
||
public function specifiers(int $i) { | ||
// https://3v4l.org/fmVIg | ||
assertType('non-falsy-string', sprintf('%14s', $i)); | ||
assertType('numeric-string', sprintf('%14s', $i)); | ||
|
||
assertType('numeric-string', sprintf('%d', $i)); | ||
|
||
|
@@ -45,9 +45,19 @@ public function specifiers(int $i) { | |
|
||
} | ||
|
||
public function positionalArgs($mixed, int $i, float $f, string $s) { | ||
/** | ||
* @param positive-int $posInt | ||
* @param negative-int $negInt | ||
* @param int<1, 5> $nonZeroIntRange | ||
* @param int<-1, 5> $intRange | ||
*/ | ||
public function positionalArgs($mixed, int $i, float $f, string $s, int $posInt, int $negInt, int $nonZeroIntRange, int $intRange) { | ||
// https://3v4l.org/vVL0c | ||
assertType('non-falsy-string', sprintf('%2$14s', $mixed, $i)); | ||
assertType('numeric-string', sprintf('%2$14s', $mixed, $i)); | ||
assertType('non-falsy-string&numeric-string', sprintf('%2$14s', $mixed, $posInt)); | ||
assertType('non-falsy-string&numeric-string', sprintf('%2$14s', $mixed, $negInt)); | ||
Comment on lines
+57
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. improvements because of the added |
||
assertType('numeric-string', sprintf('%2$14s', $mixed, $intRange)); | ||
assertType('non-falsy-string&numeric-string', sprintf('%2$14s', $mixed, $nonZeroIntRange)); | ||
|
||
assertType('numeric-string', sprintf('%2$.14F', $mixed, $i)); | ||
assertType('numeric-string', sprintf('%2$.14F', $mixed, $f)); | ||
|
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.
instead of building the general return type first and pass it as a default-return into
getConstantType()
we now invokegetConstantType()
first and build the general return type only if we were not able to infer a constant type in the first place