Skip to content
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

Throw points (work in progress) #481

Merged
merged 10 commits into from
Mar 27, 2021
Merged

Conversation

rainbow-alex
Copy link
Contributor

As discussed in #472 I've begun implementing the 'throw points' logic in NodeScopeResolver. As you suspected @ondrejmirtes assertVariableCertainty does the trick.

So far I've only implemented throw points at a statement level, so all expressions are still behaving as if they are nothrow, only an explicit throw statement creates a point. I just started extending the implementation to the expression level and I am running into an issue. I am trying to get this test to pass:

/**
 * @throws Exception
 */
function throws()
{
}

function () {
	try {
		throws();
		$foo = 1;
	} finally {
		assertVariableCertainty(TrinaryLogic::createMaybe(), $foo);
	}
};

But I can't seem to get throws() to resolve... when processing the FuncCall node, $functionReflection is always null. I'm not sure how get it to work, I hope I won't have to go messing around in TestCase::createBroker...

@rainbow-alex rainbow-alex changed the title Throw points Throw points (work in progress) Mar 21, 2021
@ondrejmirtes
Copy link
Member

Hi, I like starting with a minimal approach so 👍 on that! I'll review it once I get to chance, if it's a cohesive set of changes please don't add anything to it meanwhile.

As for the test you can't figure out - for functions you need to require_once the file in gatherAssertTypes. That's why you've put the assert code in an anonymous function below so that's good.

Alternatively you can test classes + calling methods which thanks to autoloading doesn't require such workarounds. Don't forget to call composer dump-autoload once you add the class.

@rainbow-alex
Copy link
Contributor Author

As for the test you can't figure out - for functions you need to require_once the file in gatherAssertTypes. That's why you've put the assert code in an anonymous function below so that's good.

That works, thank you!

Alternatively you can test classes + calling methods which thanks to autoloading doesn't require such workarounds. Don't forget to call composer dump-autoload once you add the class.

I'll need to do both since FuncCall and MethodCall are separate nodes...

@rainbow-alex rainbow-alex marked this pull request as draft March 21, 2021 14:22
@ondrejmirtes
Copy link
Member

It would be great if all the tests passed before I review this. Thank you.

@ondrejmirtes
Copy link
Member

But the general direction looks promising :)

@ondrejmirtes
Copy link
Member

Please use rebase instead of merge commits, I want to keep the history linear.

@rainbow-alex rainbow-alex force-pushed the throw-points branch 2 times, most recently from 1e7ed2b to a59d356 Compare March 24, 2021 13:39
@rainbow-alex
Copy link
Contributor Author

That should be green + no merge commits @ondrejmirtes.

Still to do:

  • implement throw points for a lot of expression nodes, though some of the hardest ones (assignment and calls) are done already
  • take into account throw points for expression nodes of statements (e.g. expr in if (expr) stmts)

I had to make some adjustments to the resolution of TryCatch to prevent regressions, I tried to keep the changes as small as possible (for this PR).

@rainbow-alex
Copy link
Contributor Author

Nice! Looks like we're getting there, do you want me to add the test coverage for the nodes you implemented as well?

@ondrejmirtes
Copy link
Member

@rainbow-alex It's fine like this, I'm almost finished locally :)

@ondrejmirtes
Copy link
Member

@rainbow-alex This commit (e0182ce) is the main thing that was missing - the actual connection between throw points and specific catch blocks :)

@ondrejmirtes ondrejmirtes marked this pull request as ready for review March 27, 2021 16:54
@ondrejmirtes ondrejmirtes merged commit 940e457 into phpstan:master Mar 27, 2021
@ondrejmirtes
Copy link
Member

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants