Skip to content

Broken move semantics in decl_macro #46314

Closed

Description

@valff

Move semantics with declarative macros 2.0 are broken. Can copy non-copy types.
Playground

#![feature(decl_macro)]
#![allow(unused_macros)]

#[derive(Debug)]
struct NonCopy(String);

struct Foo {
  x: NonCopy,
}

macro copy($foo:ident) {
  $foo.x
}

macro_rules! copy2 {
  ($foo:ident) => {
    $foo.x
  }
}

fn main() {
  let foo = Foo { x: NonCopy("foo".into()) };
  assert_two_copies(copy!(foo), foo.x);
  // A similar old-style macro will not work:
  //assert_two_copies(copy2!(foo), foo.x);
}

fn assert_two_copies(a: NonCopy, b: NonCopy) {
  println!("Got two copies: {:?}, {:?}", a, b);
}

rustc --version --verbose:

rustc 1.23.0-nightly (63739ab7b 2017-11-21)
binary: rustc
commit-hash: 63739ab7b210c1a8c890c2ea5238a3284877daa3
commit-date: 2017-11-21
host: x86_64-unknown-linux-gnu
release: 1.23.0-nightly
LLVM version: 4.0

Activity

  1. kennytm commented on Nov 27, 2017

    @kennytm
    Member

    The error is caught when enabling MIR borrow-check (pass -Z borrowck-mir or -Z nll), so this is a HIR borrow-check-only issue.

  2. petrochenkov commented on Nov 27, 2017

    @petrochenkov
    Contributor

    Probably some fields are compared by name without hygiene adjustments.

  3. self-assigned this
    on Dec 1, 2017
  4. pnkfelix commented on Dec 21, 2017

    @pnkfelix
    Contributor

    (tagging with nll labels just so we remember to look at this case when double checking lexical vs NLL.)

  5. nikomatsakis commented on Apr 3, 2018

    @nikomatsakis
    Contributor

    We should file this under #47366, seems like?

  6. petrochenkov commented on Apr 3, 2018

    @petrochenkov
    Contributor

    It's very likely this is fixed on my field hygiene branch, I'll check.

  7. self-assigned this
    on Apr 3, 2018
  8. valff commented on Apr 3, 2018

    @valff
    ContributorAuthor

    @nikomatsakis Thanks for the reference. I will make a PR with a test for this bug soon.

  9. added a commit that references this issue on Apr 4, 2018
  10. added a commit that references this issue on Apr 4, 2018
  11. added a commit that references this issue on Apr 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

A-NLLArea: Non-lexical lifetimes (NLL)A-borrow-checkerArea: The borrow checkerA-decl-macros-2-0Area: Declarative macros 2.0 (#39412)C-bugCategory: This is a bug.P-mediumMedium priority

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions