Skip to content

Commit aa91900

Browse files
committed
Rollup merge of #22278 - nikomatsakis:rustc-error, r=nikomatsakis
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. r? @pnkfelix
2 parents ad1cec6 + fb05f28 commit aa91900

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

src/librustc/lint/builtin.rs

+1
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,7 @@ impl LintPass for UnusedAttributes {
674674
"stable",
675675
"unstable",
676676
"rustc_on_unimplemented",
677+
"rustc_error",
677678

678679
// FIXME: #19470 this shouldn't be needed forever
679680
"old_orphan_check",

src/librustc_trans/trans/base.rs

+8
Original file line numberDiff line numberDiff line change
@@ -2425,6 +2425,14 @@ fn finish_register_fn(ccx: &CrateContext, sp: Span, sym: String, node_id: ast::N
24252425

24262426

24272427
if is_entry_fn(ccx.sess(), node_id) {
2428+
// check for the #[rustc_error] annotation, which forces an
2429+
// error in trans. This is used to write compile-fail tests
2430+
// that actually test that compilation succeeds without
2431+
// reporting an error.
2432+
if ty::has_attr(ccx.tcx(), local_def(node_id), "rustc_error") {
2433+
ccx.tcx().sess.span_fatal(sp, "compilation successful");
2434+
}
2435+
24282436
create_entry_wrapper(ccx, sp, llfn);
24292437
}
24302438
}

src/test/compile-fail/rustc-error.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#[rustc_error]
12+
fn main() {
13+
//~^ ERROR compilation successful
14+
}

0 commit comments

Comments
 (0)