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

Using an interpolated variable by-ref twice in a macro causes borrowck errors #8367

Closed
huonw opened this issue Aug 7, 2013 · 0 comments
Closed
Labels
A-syntaxext Area: Syntax extensions

Comments

@huonw
Copy link
Member

huonw commented Aug 7, 2013

Using only the by_val line compiles fine:

trait Foo {
    fn by_val(self) {}
    fn by_ref(&self) {}
}
impl Foo for int {}

fn main() {
    macro_rules! t (
        ($a:expr) => { {
            // $a.by_val(); $a.by_val();
            $a.by_ref(); $a.by_ref();
        } }
    );

    t!(1i);
}
macro-use-twice.rs:15:7: 15:9 error: borrowed value does not live long enough
macro-use-twice.rs:15     t!(1i);
                             ^~
macro-use-twice.rs:8:4: 13:6 note: in expansion of t!
macro-use-twice.rs:15:4: 15:11 note: expansion site
macro-use-twice.rs:9:23: 12:9 note: borrowed pointer must be valid for the block at 9:23...
macro-use-twice.rs:9         ($a:expr) => { {
macro-use-twice.rs:10             //$a.by_val(); $a.by_val();
macro-use-twice.rs:11             $a.by_ref(); $a.by_ref();
macro-use-twice.rs:12         } }
macro-use-twice.rs:15:7: 11:37 note: ...but borrowed value is only valid for the method call at 15:7
macro-use-twice.rs:8:4: 13:6 note: in expansion of t!
macro-use-twice.rs:15:4: 15:11 note: expansion site
macro-use-twice.rs:15:7: 15:9 error: borrowed value does not live long enough
macro-use-twice.rs:15     t!(1i);
                             ^~
macro-use-twice.rs:8:4: 13:6 note: in expansion of t!
macro-use-twice.rs:15:4: 15:11 note: expansion site
macro-use-twice.rs:9:23: 12:9 note: borrowed pointer must be valid for the block at 9:23...
macro-use-twice.rs:9         ($a:expr) => { {
macro-use-twice.rs:10             //$a.by_val(); $a.by_val();
macro-use-twice.rs:11             $a.by_ref(); $a.by_ref();
macro-use-twice.rs:12         } }
macro-use-twice.rs:15:7: 11:37 note: ...but borrowed value is only valid for the method call at 15:7
macro-use-twice.rs:8:4: 13:6 note: in expansion of t!
macro-use-twice.rs:15:4: 15:11 note: expansion site
error: aborting due to 2 previous errors
nikomatsakis added a commit to nikomatsakis/rust that referenced this issue Sep 7, 2013
…node has a

unique id. Fixes numerous bugs in macro expansion and deriving. Add two
representative tests.

Fixes rust-lang#7971
Fixes rust-lang#6304
Fixes rust-lang#8367
Fixes rust-lang#8754
Fixes rust-lang#8852
bors added a commit that referenced this issue Sep 10, 2013
…r=catamorphism

Ensures that each AST node has a unique id. Fixes numerous bugs in macro expansion and deriving. Add two
representative tests.

Fixes #7971
Fixes #6304
Fixes #8367
Fixes #8754
Fixes #8852
Fixes #2543
Fixes #7654
flip1995 pushed a commit to flip1995/rust that referenced this issue Feb 24, 2022
Don't lint `needless_borrow` in method receiver positions

fixes rust-lang#8408
fixes rust-lang#8407
fixes rust-lang#8391
fixes rust-lang#8367
fixes rust-lang#8380

This is a temporary fix for `needless_borrow`. The proper fix is included in rust-lang#8355.

This should probably be merged into rustc before beta branches on Friday. This issue has been reported six or seven times in the past couple of weeks.

changelog: Fix various issues with `needless_borrow` n´. Note to changelog writer: those issues might have been introduced in this release cycle, so this might not matter in the changelog.
flip1995 pushed a commit to flip1995/rust that referenced this issue Jun 30, 2022
Add lint `explicit_auto_deref` take 2

fixes: rust-lang#234
fixes: rust-lang#8367
fixes: rust-lang#8380

Still things to do:

* ~~This currently only lints `&*<expr>` when it doesn't trigger `needless_borrow`.~~
* ~~This requires a borrow after a deref to trigger. So `*<expr>` changing `&&T` to `&T` won't be caught.~~
* The `deref` and `deref_mut` trait methods aren't linted.
* Neither ~~field accesses~~, nor method receivers are linted.
* ~~This probably shouldn't lint reborrowing.~~
* Full slicing to deref should probably be handled here as well. e.g. `&vec[..]` when just `&vec` would do

changelog: new lint `explicit_auto_deref`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-syntaxext Area: Syntax extensions
Projects
None yet
1 participant