You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The copy mutates pragma, which indicates that a type is changed when it's copied (e.g., copying a nilable owned resets the copied-from variable to nil). This pragma is propagated to records that contain owned fields, etc. This propagation continues even if normally-defined type (e.g., set in the standard library) defines a non-mutating copy constructor via init=. As a result, types like set are considered copy mutates and copying them requires a mutable variable.
Thus, whereas the following program works:
record R {
var containedValue;
}
const x =new R(42);
var y = x;
writeln((x, y));
The following program does not:
use Set;
const x =new set(int);
var y = x;
writeln((x, y));
Printing the following error:
setcopy.chpl:1: In module 'setcopy':
setcopy.chpl:9: error: argument 1 for tuple construction is const but tuple construction takes ownership
Note that this is specifically caused by the fact that copying into the tuple would mutate the value (as far as the compiler believes), but this is not possible.
DanilaFe
changed the title
Compiler: the copy mutates pragma is set too aggressively
Compiler considers some immutably-copyable types non-immutably-copyable
Nov 13, 2024
recordContainingCopyMutatesField should return a boolean indicating whether the record copy mutates a field. Instead of querying to hasFlag(FLAG_COPY_MUTATES), we should invoke recordContainingCopyMutatesField(). This is analogous to propagateNotPOD().
We have FLAG_TYPE_INIT_EQUAL_FROM_REF and FLAG_TYPE_INIT_EQUAL_FROM_CONST, which get calculated and set upon calling setRecordCopyableFlags(). Consider merging the former flag with FLAG_COPY_MUTATES.
I don't have a reproducer yet but I have been running into an issue that might be related with a record containing a type t; var field: t instantiated with nothing/none.
If I remember right, @dlongnecke-cray did some work to make sure collection types supported containing classes correctly and might have some insights into why things are the way they are, if you haven't checked with him already
The
copy mutates
pragma, which indicates that a type is changed when it's copied (e.g., copying a nilableowned
resets the copied-from variable tonil
). This pragma is propagated to records that containowned
fields, etc. This propagation continues even if normally-defined type (e.g.,set
in the standard library) defines a non-mutating copy constructor viainit=
. As a result, types likeset
are consideredcopy mutates
and copying them requires a mutable variable.Thus, whereas the following program works:
The following program does not:
Printing the following error:
Note that this is specifically caused by the fact that copying into the tuple would mutate the value (as far as the compiler believes), but this is not possible.
chapel/compiler/resolution/lateConstCheck.cpp
Lines 715 to 721 in e1b1d03
The issue is that
recordContainingCopyMutatesField
incorrectly appliesCOPY_MUTATES
even if a non-mutatinginit=
is defined for the type.chapel/compiler/resolution/resolveFunction.cpp
Line 392 in e1b1d03
The text was updated successfully, but these errors were encountered: