path-resolution.rst: Rewrite relative import normalizatin rules to make them more precise

This commit is contained in:
Kamil Śliwak 2022-09-27 15:34:29 +02:00
parent ac5d575e9c
commit 1df95ed552

View File

@ -248,19 +248,14 @@ and is bounded by two path separators.
A separator is a forward slash or the beginning/end of the string. A separator is a forward slash or the beginning/end of the string.
For example in ``./abc/..//`` there are three path segments: ``.``, ``abc`` and ``..``. For example in ``./abc/..//`` there are three path segments: ``.``, ``abc`` and ``..``.
The compiler computes a source unit name from the import path in the following way: The compiler resolves the import into a source unit name based on the import path, in the following way:
1. First a prefix is computed #. We start with the source unit name of the importing source unit.
#. The last path segment with preceding slashes is removed from the resolved name.
- Prefix is initialized with the source unit name of the importing source unit. #. Then, for every segment in the import path, starting from the leftmost one:
- The last path segment with preceding slashes is removed from the prefix. - If the segment is ``.``, it is skipped.
- Then, the leading part of the normalized import path, consisting only of ``/`` and ``.`` - If the segment is ``..``, the last path segment with preceding slashes is removed from the resolved name.
characters is considered. - Otherwise, the segment (preceded by a single slash if the resolved name is not empty), is appended to the resolved name.
For every ``..`` segment found in this part the last path segment with preceding slashes is
removed from the prefix.
2. Then the prefix is prepended to the normalized import path.
If the prefix is non-empty, a single slash is inserted between it and the import path.
The removal of the last path segment with preceding slashes is understood to The removal of the last path segment with preceding slashes is understood to
work as follows: work as follows:
@ -268,14 +263,10 @@ work as follows:
1. Everything past the last slash is removed (i.e. ``a/b//c.sol`` becomes ``a/b//``). 1. Everything past the last slash is removed (i.e. ``a/b//c.sol`` becomes ``a/b//``).
2. All trailing slashes are removed (i.e. ``a/b//`` becomes ``a/b``). 2. All trailing slashes are removed (i.e. ``a/b//`` becomes ``a/b``).
The normalization rules are the same as for UNIX paths, namely: Note that the process normalizes the part of the resolved source unit name that comes from the import path according
to the usual rules for UNIX paths, i.e. all ``.`` and ``..`` are removed and multiple slashes are
- All the internal ``.`` segments are removed. squashed into a single one.
- Every internal ``..`` segment backtracks one level up in the hierarchy. On the other hand, the part that comes from the source unit name of the importing module remains unnormalized.
- Multiple slashes are squashed into a single one.
Note that normalization is performed only on the import path.
The source unit name of the importing module that is used for the prefix remains unnormalized.
This ensures that the ``protocol://`` part does not turn into ``protocol:/`` if the importing file This ensures that the ``protocol://`` part does not turn into ``protocol:/`` if the importing file
is identified with a URL. is identified with a URL.