[go: nahoru, domu]

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

Improvements to function and record inference #1586

Open
wants to merge 2 commits into
base: has-argument
Choose a base branch
from

Conversation

oxisto
Copy link
Member
@oxisto oxisto commented Jun 11, 2024

This PR achieves several things:

  • We try to infer the return types of function based on their context
  • A simple heuristic to better decide whether a function should be inferred in a class or in a global scope
  • A simple heuristic to decide whether a record or a namespace should be inferred
  • We recursively infer nested namespaces, e.g. A::B::C

@oxisto oxisto changed the base branch from main to 2nd-try-type-normalisation June 11, 2024 10:40
@oxisto oxisto changed the title experimental inference return type Trying to infer return type of functions Jun 11, 2024
@oxisto oxisto force-pushed the experimental-inference-return-type branch from 6319ab6 to 5391395 Compare June 11, 2024 11:24
@oxisto oxisto changed the title Trying to infer return type of functions Improvements to function inference Jun 12, 2024
@oxisto oxisto force-pushed the 2nd-try-type-normalisation branch from 9ee49ac to abee755 Compare June 12, 2024 16:18
@oxisto oxisto force-pushed the experimental-inference-return-type branch 2 times, most recently from 2abc11d to fed387d Compare June 12, 2024 22:25
@oxisto oxisto force-pushed the 2nd-try-type-normalisation branch from abee755 to b101506 Compare June 13, 2024 13:02
@oxisto oxisto force-pushed the experimental-inference-return-type branch from 31dcfad to 7f79c38 Compare June 19, 2024 06:52
@oxisto oxisto changed the base branch from 2nd-try-type-normalisation to main June 19, 2024 06:53
@oxisto oxisto changed the base branch from main to 2nd-try-type-normalisation June 19, 2024 06:53
@oxisto oxisto force-pushed the experimental-inference-return-type branch from 7f79c38 to 9199f61 Compare June 19, 2024 06:53
@oxisto oxisto force-pushed the 2nd-try-type-normalisation branch from b101506 to 25e6dc9 Compare June 19, 2024 06:54
@oxisto oxisto force-pushed the experimental-inference-return-type branch from 9199f61 to ed0aec5 Compare June 19, 2024 06:54
@oxisto oxisto force-pushed the 2nd-try-type-normalisation branch 2 times, most recently from bb4e009 to 196265d Compare June 20, 2024 09:01
@oxisto oxisto changed the title Improvements to function inference Improvements to function and record inference Jun 20, 2024
@oxisto oxisto force-pushed the experimental-inference-return-type branch 2 times, most recently from 9b2ec8b to 0693ce2 Compare June 26, 2024 11:53
@oxisto oxisto force-pushed the 2nd-try-type-normalisation branch 2 times, most recently from 40f1e1a to 19744c3 Compare June 27, 2024 16:16
@oxisto oxisto force-pushed the experimental-inference-return-type branch 2 times, most recently from ec377d6 to 94e7940 Compare June 28, 2024 19:50
Base automatically changed from 2nd-try-type-normalisation to main July 2, 2024 14:21
@oxisto oxisto force-pushed the experimental-inference-return-type branch from 94e7940 to 25f505f Compare July 3, 2024 06:26
@oxisto oxisto marked this pull request as ready for review July 3, 2024 06:32
* A very EXPERIMENTAL feature. If this is enabled, we will try to infer return types of
* functions based on the context of the call it originated out of. This is disabled by default.
*/
val inferReturnTypes: Boolean,
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 am somewhat satisfied with this feature, so we could also enable this by default

@@ -68,4 +68,6 @@ interface ArgumentHolder : Holder<Expression> {
operator fun minusAssign(node: Expression) {
removeArgument(node)
}

fun hasArgument(expression: Expression): Boolean
Copy link
Member Author

Choose a reason for hiding this comment

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

@konradweiss this is needed in order to check the context of a call expression in order to derive the function type. But adding this "polutes" the PR somewhat because I need to change all files that implement ArgumentHolder. I guess it would make sense to have this in a separate PR?

// the following function are more for type-resolving, but they are placed inside the
// symbol resolver, because this allows us to have the necessary location of nodes where
// we infer from
is RecordDeclaration -> handleRecordDeclaration(node)
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 need to check, I think this is not necessary anymore, this should already be handled by the type resolver now.

val root = it.root as? ObjectType
root?.recordDeclaration
}
fun handleRecordDeclaration(record: RecordDeclaration) {
Copy link
Member Author

Choose a reason for hiding this comment

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

see above, I think this is not needed anymore

@@ -528,6 +544,52 @@ class Inference internal constructor(val start: Node, override val ctx: Translat
this.scopeManager = ctx.scopeManager
this.typeManager = ctx.typeManager
}

fun inferReturnType(call: CallExpression?): Type {
Copy link
Member Author

Choose a reason for hiding this comment

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

kdoc

@@ -605,3 +667,197 @@ fun RecordDeclaration.inferMethod(
call
) as? MethodDeclaration
}

fun TranslationContext.tryNamespaceInference(
Copy link
Member Author

Choose a reason for hiding this comment

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

kdoc

* Tries to infer a [RecordDeclaration] from an unresolved [Type]. This will return `null`, if
* inference was not possible, or if it was turned off in the [InferenceConfiguration].
*/
fun TranslationContext.tryRecordInference(
Copy link
Member Author

Choose a reason for hiding this comment

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

add a quick comment that we are also inferring the (nested) scope of the record, if it does not exist

return methods.any { it.name.localName == name }
}

fun TranslationContext.tryParentScopeInference(
Copy link
Member Author

Choose a reason for hiding this comment

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

kdoc

@@ -58,7 +58,7 @@ class CXXAmbiguitiesTest {
assertNotNull(tu)

// make sure we still have only one declaration in the file (the record)
assertEquals(1, tu.declarations.size)
assertEquals(3, tu.declarations.size)
Copy link
Member Author

Choose a reason for hiding this comment

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

need to double-check why it is 3 now

This adds a small utility function to `ArgumentHolder`, so we can check if an expression is part of their "arguments".
@oxisto oxisto force-pushed the experimental-inference-return-type branch from 4c5cdca to e90e715 Compare July 3, 2024 06:44
@oxisto oxisto force-pushed the experimental-inference-return-type branch from e90e715 to 4932845 Compare July 3, 2024 06:44
@oxisto oxisto changed the base branch from main to has-argument July 3, 2024 06:44
Copy link
sonarcloud bot commented Jul 3, 2024

@oxisto oxisto force-pushed the has-argument branch 2 times, most recently from 2ff4961 to 0e7fcb6 Compare July 4, 2024 06:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant