added integer promotion, implicit upcast

This commit is contained in:
davu 2023-02-21 21:10:16 +01:00 committed by Jan Jurzitza
parent cad3f3d747
commit 086fc5bd73
11 changed files with 47 additions and 2 deletions

View File

@ -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"));
}

View File

@ -0,0 +1 @@
/home/davu/code/repos/vushu/DCD/tests/tc_locate_ufcs_function/barutils/barutils.d 22

View File

@ -0,0 +1,2 @@
identifiers
world v World world /home/davu/code/repos/vushu/DCD/tests/tc_recursive_public_import/testing/a.d 77

View File

@ -6,4 +6,5 @@ max k
min k
sizeof k
someBool F
someInt F
stringof k

View File

@ -6,4 +6,5 @@ max k
min k
sizeof k
someByte F
someInt F
stringof k

View File

@ -6,4 +6,5 @@ max k
min k
sizeof k
someChar F
someInt F
stringof k

View File

@ -6,4 +6,5 @@ max k
min k
sizeof k
someDchar F
someUint F
stringof k

View File

@ -5,5 +5,6 @@ mangleof k
max k
min k
sizeof k
someInt F
someShort F
stringof k

View File

@ -5,5 +5,6 @@ mangleof k
max k
min k
sizeof k
someInt F
someUbyte F
stringof k

View File

@ -5,5 +5,6 @@ mangleof k
max k
min k
sizeof k
someInt F
someUshort F
stringof k

View File

@ -5,5 +5,6 @@ mangleof k
max k
min k
sizeof k
someInt F
someWchar F
stringof k