From 086fc5bd73ace62cdd5f283c22c13a01ec2238b4 Mon Sep 17 00:00:00 2001 From: davu Date: Tue, 21 Feb 2023 21:10:16 +0100 Subject: [PATCH] added integer promotion, implicit upcast --- src/dcd/server/autocomplete/ufcs.d | 38 ++++++++++++++++++- tests/tc_locate_ufcs_function/expected.txt | 1 + tests/tc_recursive_public_import/expected.txt | 2 + .../expected_bool_test.txt | 1 + .../expected_byte_test.txt | 1 + .../expected_char_test.txt | 1 + .../expected_dchar_test.txt | 1 + .../expected_short_test.txt | 1 + .../expected_ubyte_test.txt | 1 + .../expected_ushort_test.txt | 1 + .../expected_wchar_test.txt | 1 + 11 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 tests/tc_locate_ufcs_function/expected.txt create mode 100644 tests/tc_recursive_public_import/expected.txt diff --git a/src/dcd/server/autocomplete/ufcs.d b/src/dcd/server/autocomplete/ufcs.d index b6ba298..2d1f386 100644 --- a/src/dcd/server/autocomplete/ufcs.d +++ b/src/dcd/server/autocomplete/ufcs.d @@ -13,6 +13,19 @@ import std.string; import dparse.lexer : tok; import std.regex; import containers.hashset : HashSet; +import dparse.ast; + +// https://dlang.org/spec/type.html#implicit-conversions +enum string[string] INTEGER_PROMOTIONS = [ + "bool": "int", + "byte": "int", + "ubyte": "int", + "short": "int", + "ushort": "int", + "char": "int", + "wchar": "int", + "dchar": "uint", + ]; void lookupUFCS(Scope* completionScope, DSymbol* beforeDotSymbol, size_t cursorPosition, ref AutocompleteResponse response) { @@ -106,6 +119,19 @@ DSymbol*[] getSymbolsForUFCS(Scope* completionScope, const(DSymbol)* beforeDotSy return localAppender.opSlice ~ globalAppender.opSlice; } +bool willImplicitBeUpcasted(string from, string to) +{ + import std.stdio; + + string* found = from in INTEGER_PROMOTIONS; + if (!found) + { + return false; + } + + return INTEGER_PROMOTIONS[from] == to; +} + /** * Params: * incomingSymbol = the function symbol to check if it is valid for UFCS with `beforeDotType`. @@ -126,7 +152,8 @@ bool isCallableWithArg(const(DSymbol)* incomingSymbol, const(DSymbol)* beforeDot if (incomingSymbol.kind == CompletionKind.functionName && !incomingSymbol .functionParameters.empty) { - return incomingSymbol.functionParameters.front.type is beforeDotType; + return beforeDotType is incomingSymbol.functionParameters.front.type || + willImplicitBeUpcasted(beforeDotType.name, incomingSymbol.functionParameters.front.type.name); } return false; @@ -180,7 +207,8 @@ void getUFCSParenCompletion(ref DSymbol*[] symbols, Scope* completionScope, istr return; DSymbol*[] possibleUFCSSymbol = completionScope.getSymbolsByNameAndCursor(nextToken, cursorPosition); - foreach(nextSymbol; possibleUFCSSymbol){ + foreach (nextSymbol; possibleUFCSSymbol) + { if (nextSymbol && nextSymbol.functionParameters) { if (nextSymbol.isCallableWithArg(firstSymbol.type)) @@ -191,3 +219,9 @@ void getUFCSParenCompletion(ref DSymbol*[] symbols, Scope* completionScope, istr } } } + +unittest +{ + assert(!willImplicitBeUpcasted("A", "B")); + assert(willImplicitBeUpcasted("bool", "int")); +} diff --git a/tests/tc_locate_ufcs_function/expected.txt b/tests/tc_locate_ufcs_function/expected.txt new file mode 100644 index 0000000..966bec2 --- /dev/null +++ b/tests/tc_locate_ufcs_function/expected.txt @@ -0,0 +1 @@ +/home/davu/code/repos/vushu/DCD/tests/tc_locate_ufcs_function/barutils/barutils.d 22 diff --git a/tests/tc_recursive_public_import/expected.txt b/tests/tc_recursive_public_import/expected.txt new file mode 100644 index 0000000..3127e7d --- /dev/null +++ b/tests/tc_recursive_public_import/expected.txt @@ -0,0 +1,2 @@ +identifiers +world v World world /home/davu/code/repos/vushu/DCD/tests/tc_recursive_public_import/testing/a.d 77 diff --git a/tests/tc_ufcs_fundamental_types_completion/expected_bool_test.txt b/tests/tc_ufcs_fundamental_types_completion/expected_bool_test.txt index fa7e821..36be1a9 100644 --- a/tests/tc_ufcs_fundamental_types_completion/expected_bool_test.txt +++ b/tests/tc_ufcs_fundamental_types_completion/expected_bool_test.txt @@ -6,4 +6,5 @@ max k min k sizeof k someBool F +someInt F stringof k diff --git a/tests/tc_ufcs_fundamental_types_completion/expected_byte_test.txt b/tests/tc_ufcs_fundamental_types_completion/expected_byte_test.txt index 81f3a2a..7e90a71 100644 --- a/tests/tc_ufcs_fundamental_types_completion/expected_byte_test.txt +++ b/tests/tc_ufcs_fundamental_types_completion/expected_byte_test.txt @@ -6,4 +6,5 @@ max k min k sizeof k someByte F +someInt F stringof k diff --git a/tests/tc_ufcs_fundamental_types_completion/expected_char_test.txt b/tests/tc_ufcs_fundamental_types_completion/expected_char_test.txt index 6034f3b..4cbcf1b 100644 --- a/tests/tc_ufcs_fundamental_types_completion/expected_char_test.txt +++ b/tests/tc_ufcs_fundamental_types_completion/expected_char_test.txt @@ -6,4 +6,5 @@ max k min k sizeof k someChar F +someInt F stringof k diff --git a/tests/tc_ufcs_fundamental_types_completion/expected_dchar_test.txt b/tests/tc_ufcs_fundamental_types_completion/expected_dchar_test.txt index 86a7d83..4525617 100644 --- a/tests/tc_ufcs_fundamental_types_completion/expected_dchar_test.txt +++ b/tests/tc_ufcs_fundamental_types_completion/expected_dchar_test.txt @@ -6,4 +6,5 @@ max k min k sizeof k someDchar F +someUint F stringof k diff --git a/tests/tc_ufcs_fundamental_types_completion/expected_short_test.txt b/tests/tc_ufcs_fundamental_types_completion/expected_short_test.txt index 6e9b428..8f005e4 100644 --- a/tests/tc_ufcs_fundamental_types_completion/expected_short_test.txt +++ b/tests/tc_ufcs_fundamental_types_completion/expected_short_test.txt @@ -5,5 +5,6 @@ mangleof k max k min k sizeof k +someInt F someShort F stringof k diff --git a/tests/tc_ufcs_fundamental_types_completion/expected_ubyte_test.txt b/tests/tc_ufcs_fundamental_types_completion/expected_ubyte_test.txt index 864aa0d..e1d7fe6 100644 --- a/tests/tc_ufcs_fundamental_types_completion/expected_ubyte_test.txt +++ b/tests/tc_ufcs_fundamental_types_completion/expected_ubyte_test.txt @@ -5,5 +5,6 @@ mangleof k max k min k sizeof k +someInt F someUbyte F stringof k diff --git a/tests/tc_ufcs_fundamental_types_completion/expected_ushort_test.txt b/tests/tc_ufcs_fundamental_types_completion/expected_ushort_test.txt index ecf0f61..1d75a22 100644 --- a/tests/tc_ufcs_fundamental_types_completion/expected_ushort_test.txt +++ b/tests/tc_ufcs_fundamental_types_completion/expected_ushort_test.txt @@ -5,5 +5,6 @@ mangleof k max k min k sizeof k +someInt F someUshort F stringof k diff --git a/tests/tc_ufcs_fundamental_types_completion/expected_wchar_test.txt b/tests/tc_ufcs_fundamental_types_completion/expected_wchar_test.txt index 29fd994..7989ae3 100644 --- a/tests/tc_ufcs_fundamental_types_completion/expected_wchar_test.txt +++ b/tests/tc_ufcs_fundamental_types_completion/expected_wchar_test.txt @@ -5,5 +5,6 @@ mangleof k max k min k sizeof k +someInt F someWchar F stringof k