move-language / move

Apache License 2.0
2.26k stars 689 forks source link

[borrow v2] Alternative design sketch using a checked reference types #837

Open wrwg opened 1 year ago

wrwg commented 1 year ago

This sketches an alternative design to #835 based on new types global::Ref<T> and global::RefMut<T>. Those types are considered to be known to the compiler, but some checking is defererred to runtime.

I'm not sure yet whether this design is correct, but I think it points at least in the right direction.

wrwg commented 1 year ago

@chen-robert

chen-robert commented 1 year ago

Minor typo nit, I think ref_mut has an extra reference on rc right?

public native fun ref_mut<T>(rc: &RefMut<T>): &mut T /* acquires T */;

should be public native fun ref_mut(rc: RefMut): &mut T / acquires T /;

I also wonder if it would make more sense to have these as bytecode instructions instead of native functions (keeping in mind that it would be a far more invasive change). It seems like, in order to add this functionality, there need to be large changes in the bytecode verifier. For example, the acquires semantics in the borrow checker and link-time checks for T ownership.

Also, I think T cannot be a runtime type generic right?