Skip to content

ref without mut check not done on methods and associated functions #7623

@ironcev

Description

@ironcev

Related to #7621.

Officially, according to the docs we do not support ref without mut:

Note It is not currently allowed to use mut without ref or vice versa for a function parameter.

I don't know the historical reasons for this restriction. What is obvious is that the checks for it are not done properly.

E.g., in this example:

script;

struct S {}

impl S {
    fn method(self, ref _x: u64) { }    // <<<<---- No errors.
    fn associated_fun(ref _x: u64) { }  // <<<<---- No errors.
} 

fn ref_pass(ref _x: u64) { } // <<<<---- No error here, but confusing one at call site.

fn main() -> u64 {
    let x = 42;

    ref_pass(x);
    //       ^ Cannot pass immutable argument to mutable parameter.

    S {}.method(x);
    S::associated_fun(x);

    x
}

Note that using only mut without ref produces error in all cases, explaining that mut cannot be used without ref.

I am fine with supporting ref without mut assuming the effort is negligible (because we will make ref mut obsolete with references). But we have to properly implement it, document it etc. It must not be that ref without mut "works" in certain cases only because it is not properly checked.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingcompilerGeneral compiler. Should eventually become more specific as the issue is triagedcompiler: frontendEverything to do with type checking, control flow analysis, and everything between parsing and IRgen

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions