Skip to content

Commit

Permalink
[NF] Improve errors for binding type mismatches.
Browse files Browse the repository at this point in the history
- Added more specific errors for the case where a scalar is given an
  array binding, and for the case where the element types matches but
  the dimensions don't.

Belonging to [master]:
  - OpenModelica/OMCompiler#2957
  - OpenModelica/OpenModelica-testsuite#1130
  • Loading branch information
perost authored and OpenModelica-Hudson committed Mar 1, 2019
1 parent 2ba9e4b commit b691eb1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
40 changes: 37 additions & 3 deletions Compiler/NFFrontEnd/NFTypeCheck.mo
Expand Up @@ -2980,9 +2980,7 @@ algorithm
(exp, ty, ty_match) := matchTypes(binding.bindingType, comp_ty, binding.bindingExp, true);

if not isValidAssignmentMatch(ty_match) then
Error.addMultiSourceMessage(Error.VARIABLE_BINDING_TYPE_MISMATCH,
{name, Binding.toString(binding), Type.toString(comp_ty),
Type.toString(binding.bindingType)}, {Binding.getInfo(binding), InstNode.info(component)});
printBindingTypeError(name, binding, comp_ty, binding.bindingType, component);
fail();
elseif isCastMatch(ty_match) then
binding := Binding.TYPED_BINDING(exp, ty, binding.variability, binding.parents, binding.isEach, binding.evaluated, binding.isFlattened, binding.info);
Expand All @@ -3000,6 +2998,42 @@ algorithm
end match;
end matchBinding;

function printBindingTypeError
input String name;
input Binding binding;
input Type componentType;
input Type bindingType;
input InstNode component;
protected
SourceInfo binding_info, comp_info;
String bind_ty_str, comp_ty_str;
MatchKind mk;
algorithm
binding_info := Binding.getInfo(binding);
comp_info := InstNode.info(component);

if Type.isScalar(bindingType) and Type.isArray(componentType) then
Error.addMultiSourceMessage(Error.MODIFIER_NON_ARRAY_TYPE_ERROR,
{Binding.toString(binding), name}, {binding_info, comp_info});
else
(_, _, mk) := matchTypes(Type.arrayElementType(bindingType),
Type.arrayElementType(componentType),
Expression.EMPTY(bindingType), true);

if isValidAssignmentMatch(mk) then
Error.addMultiSourceMessage(Error.VARIABLE_BINDING_DIMS_MISMATCH,
{name, Binding.toString(binding),
Dimension.toStringList(Type.arrayDims(componentType)),
Dimension.toStringList(Type.arrayDims(bindingType))},
{binding_info, comp_info});
else
Error.addMultiSourceMessage(Error.VARIABLE_BINDING_TYPE_MISMATCH,
{name, Binding.toString(binding), Type.toString(componentType),
Type.toString(bindingType)}, {binding_info, comp_info});
end if;
end if;
end printBindingTypeError;

function checkDimensionType
"Checks that an expression used as a dimension has a valid type for a
dimension, otherwise prints an error and fails."
Expand Down
4 changes: 4 additions & 0 deletions Compiler/Util/Error.mo
Expand Up @@ -820,6 +820,10 @@ public constant Message INVALID_COMPONENT_PREFIX = MESSAGE(345, TRANSLATION(), E
Util.gettext("Prefix ‘%s‘ on component ‘%s‘ not allowed in class specialization ‘%s‘."));
public constant Message INVALID_CARDINALITY_CONTEXT = MESSAGE(346, TRANSLATION(), ERROR(),
Util.gettext("cardinality may only be used in the condition of an if-statement/equation or an assert."));
public constant Message VARIABLE_BINDING_DIMS_MISMATCH = MESSAGE(347, TRANSLATION(), ERROR(),
Util.gettext("Type mismatch in binding ‘%s = %s‘, expected array dimensions %s, got %s."));
public constant Message MODIFIER_NON_ARRAY_TYPE_ERROR = MESSAGE(348, TRANSLATION(), ERROR(),
Util.gettext("Non-array modification ‘%s‘ for array component ‘%s‘, possibly due to missing ‘each‘."));
public constant Message INITIALIZATION_NOT_FULLY_SPECIFIED = MESSAGE(496, TRANSLATION(), WARNING(),
Util.gettext("The initial conditions are not fully specified. %s."));
public constant Message INITIALIZATION_OVER_SPECIFIED = MESSAGE(497, TRANSLATION(), WARNING(),
Expand Down

0 comments on commit b691eb1

Please sign in to comment.