Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/dev/flang/ast/Call.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
package dev.flang.ast;

import java.util.ListIterator;
import java.util.function.Supplier;

import dev.flang.util.Errors;
import dev.flang.util.FuzionConstants;
Expand Down Expand Up @@ -2337,6 +2338,15 @@ else if (e instanceof Block b)
*/
boolean needsToInferTypeParametersFromArgs()
{
if (_generics == NO_GENERICS &&
_propagatedType != null &&
_propagatedType.isNormalType() &&
// if features do not match we will get an error later (in checktypes)
_propagatedType.feature() == _calledFeature)
{
_generics = new List<>(_propagatedType.generics());
}

return _calledFeature != null && (_generics == NO_GENERICS || _generics.stream().anyMatch(g -> g.containsUndefined(false))) && !_calledFeature.typeArguments().isEmpty();
}

Expand All @@ -2354,6 +2364,12 @@ boolean needsToInferTypeParametersFromArgs()
protected Context _resolvedFor;


/**
* The type this call should result in.
*/
private AbstractType _propagatedType;


/**
* Has this call been resolved and if so, for which context?
*
Expand Down Expand Up @@ -2677,6 +2693,34 @@ void propagateExpectedType(Resolution res, Context context)
}


/**
* During type inference: Inform this expression that it is used in an
* environment that expects the given type. In particular, if this
* expression's result is assigned to a field, this will be called with the
* type of the field.
*
* @param res this is called during type inference, res gives the resolution
* instance.
*
* @param context the source code context where this Expr is used
*
* @param t the expected type.
*
* @param from for error output: if non-null, produces a String describing
* where the expected type came from.
*
* @return either this or a new Expr that replaces thiz and produces the
* result. In particular, if the result is assigned to a temporary field, this
* will be replaced by the expression that reads the field.
*/
@Override
Expr propagateExpectedType(Resolution res, Context context, AbstractType t, Supplier<String> from)
{
_propagatedType = t;
return super.propagateExpectedType(res, context, t, from);
}


/**
* During type inference: Wrap expressions that are assigned to lazy actuals
* in functions.
Expand Down
37 changes: 37 additions & 0 deletions src/dev/flang/ast/Feature.java
Original file line number Diff line number Diff line change
Expand Up @@ -1432,6 +1432,23 @@ void resolveDeclarations(Resolution res)
}


public void propagateResultType(Resolution res)
{
// NYI: BUG: this currently does not work here:
// since resultTypeIfPresentUrgent lets numliteral fix their types
//
// var a = resultTypeIfPresentUrgent(res, false);
if (isRoutine() &&
(_returnType instanceof FunctionReturnType frt) &&
frt.functionReturnType(false) != null &&
// NYI: BUG: without this, some issue in lock_free module
!featureName().isInternal())
{
_impl.propagateExpectedType(res, context(), _returnType.functionReturnType(false));
}
}


/**
* Perform an action as soon as this feature has reached
* State.atLeast(State.RESOLVED_DECLARATIONS). Perform the action immediately
Expand Down Expand Up @@ -1542,6 +1559,26 @@ void internalResolveTypes(Resolution res)
{
_state = State.RESOLVING_TYPES;


// NYI: BUG: does not work currently
// because propagateExpectedType is misused
// to add result field currently
//
// visit(new FeatureVisitor()
// {
// @Override
// public Expr action(Feature f, AbstractFeature outer)
// {
// if (f.impl().hasInitialValue())
// {
// f.propagateResultType(res);
// }
// return super.action(f, outer);
// }
// });

propagateResultType(res);

if (Contract.requiresPreConditionsFeature(this) && preFeature() == null)
{
Contract.addPreFeature(res, this, context(), false);
Expand Down
25 changes: 25 additions & 0 deletions tests/reg_issue321/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# This file is part of the Fuzion language implementation.
#
# The Fuzion language implementation is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, version 3 of the License.
#
# The Fuzion language implementation is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU General Public License along with The
# Fuzion language implementation. If not, see <https://www.gnu.org/licenses/>.


# -----------------------------------------------------------------------
#
# Tokiwa Software GmbH, Germany
#
# Source code of Fuzion test Makefile
#
# -----------------------------------------------------------------------

override NAME = reg_issue321
include ../simple.mk
31 changes: 31 additions & 0 deletions tests/reg_issue321/reg_issue321.fz
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# This file is part of the Fuzion language implementation.
#
# The Fuzion language implementation is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, version 3 of the License.
#
# The Fuzion language implementation is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU General Public License along with The
# Fuzion language implementation. If not, see <https://www.gnu.org/licenses/>.


# -----------------------------------------------------------------------
#
# Tokiwa Software GmbH, Germany
#
# Source code of Fuzion test reg_issue321
#
# -----------------------------------------------------------------------

reg_issue321 =>

a tuple i8 i8 => tuple 3 4
# NYI: BUG: does not yet work
# b tuple i8 i8 := tuple 3 4

say a
# say b
Empty file.
1 change: 1 addition & 0 deletions tests/reg_issue321/reg_issue321.fz.expected_out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
instance[tuple i8 i8]
Loading