Opened 5 years ago

Closed 5 years ago

#5623 closed defect (fixed)

Import issue in OpenModelica

Reported by: Christian Kral <dr.christian.kral@…> Owned by: somebody
Priority: high Milestone: 1.14.0
Component: *unknown* Version: v1.14.0-dev-nightly
Keywords: Cc: georg.vorlaufer@…, Per Östlund

Description

Please consider the following package:

package ImportIssue
  model Example
    import ImportIssue.Intermediate.MyType;
    MyType var;
  equation
    var = {0,1,2,3};
  end Example;

  package Types
    type MyType = Real[4];
  end Types;

  package Intermediate
    import ImportIssue.Types.MyType;
  end Intermediate;
end ImportIssue;

The simulation example ImportIssue.Example demonstrates a detour of imports in Modelica:

In the package Intermediate the import of MyType is made. The example now imports ImportIssue.Intermediate.MyType and I am not sure if this is legal Modelica. However, if I check ImportIssue.Example OpenModelica reports:

Check of ImportIssue.Example completed successfully.
Class ImportIssue.Example has 4 equation(s) and 4 variable(s).
4 of these are trivial equation(s).

If I simulate the example, it terminates successfully.

If I check the example in Dymola, the following error occurs:

Check of ImportIssue.Example:

Error: Lookup failed for import ImportIssue.Intermediate.MyType
Near file: /home/data/work/ImportIssue.mo, line 2
Context: ImportIssue.Example

Error: Lookup failed for import ImportIssue.Intermediate.MyType
Near file: /home/data/work/ImportIssue.mo, line 2
Context: ImportIssue.Example

Component type specifier MyType not found
File: /home/data/work/ImportIssue.mo, line 5
Component context: var
Component declared as MyType var in ImportIssue.Example

ERRORS have been issued.

When compiling the example in JModelica, the following error message is caused:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/christian/bin/jmodelica20190827/Python/pymodelica/compiler.py", line 146, in compile_fmu
    separate_process, jvm_args)       
  File "/home/christian/bin/jmodelica20190827/Python/pymodelica/compiler.py", line 251, in _compile_unit
    return comp.compile_Unit(class_name, file_name, target, version, compile_to)
  File "/home/christian/bin/jmodelica20190827/Python/pymodelica/compiler_wrappers.py", line 364, in compile_Unit
    self._handle_exception(ex)
  File "/home/christian/bin/jmodelica20190827/Python/pymodelica/compiler_wrappers.py", line 544, in _handle_exception
    raise CompilerError(errors, warnings)
pymodelica.compiler_exceptions.CompilerError: 
2 error(s) and 0 warning(s) found:

Error at line 3, column 37, in file 'ImportIssue.mo':
  Cannot find class or component declaration for MyType

Error at line 5, column 7, in file 'ImportIssue.mo':
  Cannot find class declaration for MyType

Change History (4)

comment:1 by Adrian Pop, 5 years ago

Cc: Per Östlund added

I say this is legal and it should be allowed, is just another indirection in the import.
However we can ask Per on this as he knows this stuff better than me.

comment:2 by Martin Sjölund, 5 years ago

Look at 5.3.2: Only simple name lookup (the first part of the reference; in this case ImportIssue should use imported names for the lookup).

The old instantiation did this correctly. The best way to resolve this would be to detect that the name does exist and then give a clear error message to the user.

Version 0, edited 5 years ago by Martin Sjölund (next)

comment:3 by Per Östlund, 5 years ago

Yes, the specification is quite clear here. The lookup rules for composite names state that the rest of the name is looked up among the declared named elements of the class, and imports are not named elements according to 13.2.1.2.

The new frontend already keeps track of whether an element is an import or not for other reasons, so checking this restriction is easy. I've made a PR for it, #411. The NF will now give the error:

[test.mo:3:5-3:43] Error: Found imported name ‘MyType‘ while looking up composite name ‘ImportIssue.Intermediate.MyType‘.
[test.mo:4:5-4:15] Error: Class MyType not found in scope Example.

The first error is the new one, the second comes from the lookup of MyType failing because the lookup of the import failed (imports are looked up on demand in the NF).

comment:4 by Per Östlund, 5 years ago

Resolution: fixed
Status: newclosed

Fixed in f5f559a.

Note: See TracTickets for help on using tickets.