Re: [PHP-DEV] Warning for "confusable" types

This is only part of a thread. view whole thread
  107517
October 11, 2019 17:46 larry@garfieldtech.com ("Larry Garfield")
On Fri, Oct 11, 2019, at 8:54 AM, Nikita Popov wrote:
> Hi internals, > > Something I've seen play out a couple of times: Newbies try to use > something like "integer" or "resource" as a type, and then get a confusing > error message along the lines of "must be an instance of resource, resource > given". > > I would like to throw a compiler warning in this case, that looks as one of > the following: > > > Warning: "integer" will be interpreted as a class type. Did you mean > "int"? Use qualified name or "use" to suppress this warning > > > Warning: "resource" is not a supported builtin type and will be > interpreted as a class type. Use qualified name or "use" to suppress this > warning > > This warning only triggers if the type is lowercase (integer but not > Integer), is unqualified (integer but not \integer) and is not imported > (there is no "use integer"). This provides multiple ways to avoid the > warning for code that does legitimately want to use lowercase "integer" as > a class type. > > Implementation: https://github.com/php/php-src/pull/4815 > > Thoughts? > > Nikita
Can you clarify where exactly "compiler warning" would be displayed? Is that an E_WARNING? How would I as a user see a message when I write function foo(integer $a) { ... } --Larry Garfield
  107519
October 11, 2019 18:23 oludonsexy@gmail.com (Olumide Samson)
On Fri, Oct 11, 2019, 6:47 PM Larry Garfield <larry@garfieldtech.com> wrote:

> On Fri, Oct 11, 2019, at 8:54 AM, Nikita Popov wrote: > > Hi internals, > > > > Something I've seen play out a couple of times: Newbies try to use > > something like "integer" or "resource" as a type, and then get a > confusing > > error message along the lines of "must be an instance of resource, > resource > > given". > > > > I would like to throw a compiler warning in this case, that looks as one > of > > the following: > > > > > Warning: "integer" will be interpreted as a class type. Did you mean > > "int"? Use qualified name or "use" to suppress this warning > > > > > Warning: "resource" is not a supported builtin type and will be > > interpreted as a class type. Use qualified name or "use" to suppress this > > warning > > > > This warning only triggers if the type is lowercase (integer but not > > Integer), is unqualified (integer but not \integer) and is not imported > > (there is no "use integer"). This provides multiple ways to avoid the > > warning for code that does legitimately want to use lowercase "integer" > as > > a class type. > > > > Implementation: https://github.com/php/php-src/pull/4815 > > > > Thoughts? > > > > Nikita > > Can you clarify where exactly "compiler warning" would be displayed? Is > that an E_WARNING? How would I as a user see a message when I write > > function foo(integer $a) { ... } > > --Larry Garfield > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php
As a warning I think, seems I've encountered those errors many times ago. The solution Nikita propose is good, it doesn't deal with too much. These errors are there already, only just giving it a properly meaningful description. +1 for the proposed description.
  107522
October 11, 2019 18:46 bishop@php.net (Bishop Bettini)
On Fri, Oct 11, 2019 at 1:47 PM Larry Garfield <larry@garfieldtech.com>
wrote:

> On Fri, Oct 11, 2019, at 8:54 AM, Nikita Popov wrote: > > Hi internals, > > > > Something I've seen play out a couple of times: Newbies try to use > > something like "integer" or "resource" as a type, and then get a > confusing > > error message along the lines of "must be an instance of resource, > resource > > given". > > > > I would like to throw a compiler warning in this case, that looks as one > of > > the following: > > > > > Warning: "integer" will be interpreted as a class type. Did you mean > > "int"? Use qualified name or "use" to suppress this warning > > > > > Warning: "resource" is not a supported builtin type and will be > > interpreted as a class type. Use qualified name or "use" to suppress this > > warning > > > > This warning only triggers if the type is lowercase (integer but not > > Integer), is unqualified (integer but not \integer) and is not imported > > (there is no "use integer"). This provides multiple ways to avoid the > > warning for code that does legitimately want to use lowercase "integer" > as > > a class type. > > > > Implementation: https://github.com/php/php-src/pull/4815 > > > > Thoughts? > > > > Nikita > > Can you clarify where exactly "compiler warning" would be displayed? Is > that an E_WARNING? How would I as a user see a message when I write > > function foo(integer $a) { ... } >
It's an E_COMPILE_WARNING, and arises when the compiler sees you're type hinting for a class, but no such class loaded. This checks that given class against a known list of "probable mistakes" and provides guidance. Eg, I typed "integer $a" when I mean "int $a", and don't understand the current compiler warning: "Argument 1 passed to foo must be an instance of integer, int given" See also https://stackoverflow.com/a/40638544/2908724
  107525
October 11, 2019 19:21 larry@garfieldtech.com ("Larry Garfield")
On Fri, Oct 11, 2019, at 1:46 PM, Bishop Bettini wrote:
> On Fri, Oct 11, 2019 at 1:47 PM Larry Garfield <larry@garfieldtech.com> > wrote: > > > On Fri, Oct 11, 2019, at 8:54 AM, Nikita Popov wrote: > > > Hi internals, > > > > > > Something I've seen play out a couple of times: Newbies try to use > > > something like "integer" or "resource" as a type, and then get a > > confusing > > > error message along the lines of "must be an instance of resource, > > resource > > > given". > > > > > > I would like to throw a compiler warning in this case, that looks as one > > of > > > the following: > > > > > > > Warning: "integer" will be interpreted as a class type. Did you mean > > > "int"? Use qualified name or "use" to suppress this warning > > > > > > > Warning: "resource" is not a supported builtin type and will be > > > interpreted as a class type. Use qualified name or "use" to suppress this > > > warning > > > > > > This warning only triggers if the type is lowercase (integer but not > > > Integer), is unqualified (integer but not \integer) and is not imported > > > (there is no "use integer"). This provides multiple ways to avoid the > > > warning for code that does legitimately want to use lowercase "integer" > > as > > > a class type. > > > > > > Implementation: https://github.com/php/php-src/pull/4815 > > > > > > Thoughts? > > > > > > Nikita > > > > Can you clarify where exactly "compiler warning" would be displayed? Is > > that an E_WARNING? How would I as a user see a message when I write > > > > function foo(integer $a) { ... } > > > > It's an E_COMPILE_WARNING, and arises when the compiler sees you're type > hinting for a class, but no such class loaded. This checks that given class > against a known list of "probable mistakes" and provides guidance. Eg, I > typed "integer $a" when I mean "int $a", and don't understand the current > compiler warning: "Argument 1 passed to foo must be an instance of integer, > int given" > > See also https://stackoverflow.com/a/40638544/2908724
OK, so it's just taking an existing error message and customizing it in cases where we have a good idea of what the user got wrong? That's a very +1 in my book. --Larry Garfield