Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref in function parameter patterns #8860

Closed
ben0x539 opened this issue Aug 29, 2013 · 13 comments
Closed

ref in function parameter patterns #8860

ben0x539 opened this issue Aug 29, 2013 · 13 comments
Labels
E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. P-medium Medium priority
Milestone

Comments

@ben0x539
Copy link
Contributor

The following program doesn't print "drop". Looks unsound to me.

struct S; 
impl Drop for S {
    fn drop(&self) {
        println("drop"); 
    } 
} 

fn f(ref _s: S) {}

fn main() { 
    let s = S;
    f(s);
}

In #5239, @nikomatsakis suggested that ref shouldn't be allowed in argument patterns at all, but then that bug got closed because the ICE/llvm asserts over there stopped occuring.

@nikomatsakis
Copy link
Contributor

Huh. I remember putting in some effort to make sure this kind of test worked. I am not sure why I said ref should not appear in argument patterns, I don't think that anymore.

@nikomatsakis
Copy link
Contributor

Yes, there is a test src/test/run-pass/func-arg-ref-pattern.rs that is supposed to test this case. I wonder if there is a problem with the destructor on the unit struct instead?

@ben0x539
Copy link
Contributor Author

Also occurs with a non-unit struct:

struct S { i: int }
impl Drop for S {
    fn drop(&self) {
        println("drop");
    }
}

fn f(ref _s: S) {}

fn main() {
    let s = S { i: 42 };
    f(s);
}

(and either version will print "drop" if I remove the ref).

I assumed the problem with my code is that main() feels absolved of its ownership of the S because it's clearly moving it into f as that takes a parameter by value, and f doesn't feel responsible because it's only matching a ref pattern on the argument...? This seems vaguely different than what the func-arg-ref-pattern.rs test tests for, where ref isn't at the top level of the argument pattern.

@nikomatsakis
Copy link
Contributor

Presumably that is the problem. main() is correct, f() is not. f() should free its arguments upon return. In any case, clearly something IS diffierent, since one test works and the other not, though I would have thought they would be the same. Needs more investigation.

@alexcrichton
Copy link
Member

This is not good. Nominating.

@pnkfelix
Copy link
Member

Accepted for "first major release, P-high"

@edwardw
Copy link
Contributor

edwardw commented Feb 13, 2014

Both cases work for me as expected now.

$ rustc --version
rustc 0.10-pre
host: x86_64-apple-darwin

@alexcrichton
Copy link
Member

Hurray! flagging as needstest.

edwardw added a commit to edwardw/rust that referenced this issue Feb 14, 2014
alexcrichton pushed a commit to alexcrichton/rust that referenced this issue Feb 14, 2014
@edwardw
Copy link
Contributor

edwardw commented Feb 15, 2014

The issue can be closed now.

@sfackler
Copy link
Member

FYI if you put "closes #8860" in the commit message, Github will automatically close the issue when the commit merges.

@edwardw
Copy link
Contributor

edwardw commented Feb 15, 2014

I did but the PR was integrated as part of a rollup. So it didn't get closed properly.

@alexcrichton
Copy link
Member

We recommend putting the "Closes #XXX" in the commit message rather than just the PR for the reason that it'll still get closed during a rollup.

@edwardw
Copy link
Contributor

edwardw commented Feb 15, 2014

Ah, I misunderstood @sfackler's comment. The "Closes #xxxx" in both commit and PR message, now I see.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. P-medium Medium priority
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants