Skip to content

Declare tentative return types for ext/dom #6985

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

Merged
merged 3 commits into from
Jul 20, 2021

Conversation

kocsismate
Copy link
Member

No description provided.

/** @return DOMAttr|null|false */
public function setAttributeNode(DOMAttr $attr) {}
/** @tentative-return-type */
public function setAttributeNode(DOMAttr $attr): DOMAttr|null|false {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something that bothers me about the DOM case is that this puts a number of false return values into the contract that are only relevant without strict errors. Such as the one here. Once the return types are in place, dropping |false will be breaking change :/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like strictErrorChecking has actually been removed from the DOM spec, so I have some hope that we can simply drop support for it. Needs some investigation to understand what the impact is and maybe looking at some of the current uses (https://beta.grep.app/search?q=-%3EstrictErrorChecking%5Cs%2A%3D%5Cs%2Afalse&regexp=true).

@nikic
Copy link
Member

nikic commented May 14, 2021

Well, at least the curl part of this looks good to me :)

/** @return void */
public function getFeature(string $feature, string $version) {}
/** @tentative-return-type */
public function getFeature(string $feature, string $version): void {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not familiar with this functionality, but noticed randomly: I think never would be more appropriate, but because never is added in 8.1, it would (somewhat) prevent overriding this in everything older than 8.1

Then again, https://developer.mozilla.org/en-US/docs/Web/API/DOMImplementation mentions that no browser ever implemented getFeature and it was removed from the spec, so this actually makes sense, but documenting the reason for a getter returning void (in c file and .stub.php file) would avoid confusion.

https://www.php.net/domimplementation mentions the method is undocumented

Maybe keep it as : void and add @return never

Aside: Not implemented may make more sense than"Not yet implemented" but this is way out of scope.

PHP_METHOD(DOMImplementation, getFeature)
{
	size_t feature_len, version_len;
	char *feature, *version;

	if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &feature, &feature_len, &version, &version_len) == FAILURE) {
		RETURN_THROWS();
	}

	DOM_NOT_IMPLEMENTED(); // macro for zend_throw_error(NULL, "Not yet implemented");
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@beberlei Do you think this function will be ever implemented? If not, and we don't want child classes to implement it, then never would be a good choice, otherwise I'm fine with mixed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just gone with never

@kocsismate kocsismate force-pushed the tentative-return-type-2 branch from 8870fe4 to 0559df2 Compare May 18, 2021 15:50
@kocsismate
Copy link
Member Author

kocsismate commented May 18, 2021

Well, at least the curl part of this looks good to me :)

I exploded the changes into 3 separate commits, so that I can iterate on ext/dom, but apply the rest directly.

@kocsismate kocsismate force-pushed the tentative-return-type-2 branch from 0559df2 to 01e5b84 Compare May 18, 2021 15:55
@nikic
Copy link
Member

nikic commented May 26, 2021

I exploded the changes into 3 separate commits, so that I can iterate on ext/dom, but apply the rest directly.

Right, the ext/curl and ext/fileinfo parts look good to me.

kocsismate added a commit that referenced this pull request May 26, 2021
kocsismate added a commit that referenced this pull request May 26, 2021
@kocsismate kocsismate changed the title Declare tentative return types for ext/curl, ext/dom, ext/fileinfo Declare tentative return types for ext/dom May 26, 2021
@kocsismate kocsismate force-pushed the tentative-return-type-2 branch from 01e5b84 to b71d0e4 Compare May 26, 2021 10:41
@kocsismate kocsismate marked this pull request as draft May 26, 2021 10:41
@nikic
Copy link
Member

nikic commented May 26, 2021

I've been looking into this a bit, and can only say that error handling in dom is quite broken. Even with strictErrorChecking dropped there's a bunch of RETURN_FALSE that either don't match the DOM spec, or are for libxml internal errors.

For example, DOMDocument::createDocumentFragment() is spec'd to have no error conditions, but of course memory allocation in libxml can fail -- what should we be doing in that case? The current "return false" seems like a phenomenally bad way to handle it, in that people can only encounter this return value in very unusual situations that cannot be tested for. My current thinking is that we should be throwing a fatal error in this case, the same as we would do for other out-of-memory conditions in PHP.

@krakjoe krakjoe added the Tentative returns Temporary label for tentative return follow ups label May 27, 2021
@kocsismate kocsismate added this to the PHP 8.1 milestone Jul 13, 2021
@kocsismate kocsismate force-pushed the tentative-return-type-2 branch from b71d0e4 to b8c01b1 Compare July 16, 2021 20:00
@kocsismate kocsismate marked this pull request as ready for review July 17, 2021 09:52
@kocsismate
Copy link
Member Author

I've been looking into this a bit, and can only say that error handling in dom is quite broken. Even with strictErrorChecking dropped there's a bunch of RETURN_FALSE that either don't match the DOM spec, or are for libxml internal errors.

Would you be ok if we didn't add a tentative return type for methods which rely on strictErrorChecking, so that we could get rid of more false returns in the future more easily?

@nikic
Copy link
Member

nikic commented Jul 19, 2021

@kocsismate I'd be okay with adding tentative types for all methods where we currently match the DOM spec.

@kocsismate kocsismate force-pushed the tentative-return-type-2 branch from b8c01b1 to f432aa1 Compare July 19, 2021 22:43
@kocsismate
Copy link
Member Author

I'd be okay with adding tentative types for all methods where we currently match the DOM spec.

Hopefully it's done now.

@kocsismate kocsismate merged commit d9838e5 into php:master Jul 20, 2021
@kocsismate kocsismate deleted the tentative-return-type-2 branch July 20, 2021 07:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Tentative returns Temporary label for tentative return follow ups
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants