From fb05f282d7810f93c9dcb0ef979e66648a8716fc Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Thu, 12 Feb 2015 12:53:31 -0500 Subject: [PATCH] Add `#[rustc_error]` annotation, which causes trans to signal an error if found on the `main()` function. This lets you write tests that live in `compile-fail` but are expected to compile successfully. This is handy when you have many small variations on a theme that you want to keep together, and you are just testing the type checker, not the runtime semantics. --- src/librustc/lint/builtin.rs | 1 + src/librustc_trans/trans/base.rs | 8 ++++++++ src/test/compile-fail/rustc-error.rs | 14 ++++++++++++++ 3 files changed, 23 insertions(+) create mode 100644 src/test/compile-fail/rustc-error.rs diff --git a/src/librustc/lint/builtin.rs b/src/librustc/lint/builtin.rs index a415ff3ed71..0b014c4818d 100644 --- a/src/librustc/lint/builtin.rs +++ b/src/librustc/lint/builtin.rs @@ -674,6 +674,7 @@ impl LintPass for UnusedAttributes { "stable", "unstable", "rustc_on_unimplemented", + "rustc_error", // FIXME: #19470 this shouldn't be needed forever "old_orphan_check", diff --git a/src/librustc_trans/trans/base.rs b/src/librustc_trans/trans/base.rs index 52ef2b75f95..54c50b7a62b 100644 --- a/src/librustc_trans/trans/base.rs +++ b/src/librustc_trans/trans/base.rs @@ -2425,6 +2425,14 @@ fn finish_register_fn(ccx: &CrateContext, sp: Span, sym: String, node_id: ast::N if is_entry_fn(ccx.sess(), node_id) { + // check for the #[rustc_error] annotation, which forces an + // error in trans. This is used to write compile-fail tests + // that actually test that compilation succeeds without + // reporting an error. + if ty::has_attr(ccx.tcx(), local_def(node_id), "rustc_error") { + ccx.tcx().sess.span_fatal(sp, "compilation successful"); + } + create_entry_wrapper(ccx, sp, llfn); } } diff --git a/src/test/compile-fail/rustc-error.rs b/src/test/compile-fail/rustc-error.rs new file mode 100644 index 00000000000..6497439c3dc --- /dev/null +++ b/src/test/compile-fail/rustc-error.rs @@ -0,0 +1,14 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#[rustc_error] +fn main() { + //~^ ERROR compilation successful +}