Skip to content

Broken move semantics in decl_macro #46314

Closed
@valff

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

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