Skip to content

Commit d4d43e2

Browse files
committed
Auto merge of #48173 - GuillaumeGomez:error-codes-libsyntax_ext, r=estebank
Add error codes for libsyntax_ext I intend to add error codes for `libsyntax_ext` as well. However, they cannot be used at stage 0 directly so I thought it might be possible to enable them at the stage 1 only so we can have access to the macros. However, the error code registration seems to not work this way. Currently I get the following error: ``` error: used diagnostic code E0660 not registered --> libsyntax_ext/asm.rs:93:25 | 93 | span_err!(cx, sp, E0660, "malformed inline assembly"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info) error: used diagnostic code E0661 not registered --> libsyntax_ext/asm.rs:151:33 | 151 | / span_err!(cx, sp, E0661, 152 | | "output operand constraint lacks '=' or '+'"); | |________________________________________________________________________________________^ | = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info) error: aborting due to 2 previous errors error: Could not compile `syntax_ext`. ``` If anyone has an idea, I'd gladly take it. I'm trying to figure this out on my side as well. I also opened this PR to know if it was worth it to continue (maybe we don't want this?). Anyway, any answer for both questions is very welcome! cc @rust-lang/compiler
2 parents bd40cbb + f367567 commit d4d43e2

File tree

8 files changed

+118
-5
lines changed

8 files changed

+118
-5
lines changed

src/libsyntax/ext/base.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use ast::{self, Attribute, Name, PatKind, MetaItem};
1414
use attr::HasAttrs;
1515
use codemap::{self, CodeMap, Spanned, respan};
1616
use syntax_pos::{Span, MultiSpan, DUMMY_SP};
17-
use errors::DiagnosticBuilder;
17+
use errors::{DiagnosticBuilder, DiagnosticId};
1818
use ext::expand::{self, Expansion, Invocation};
1919
use ext::hygiene::{Mark, SyntaxContext};
2020
use fold::{self, Folder};
@@ -841,6 +841,9 @@ impl<'a> ExtCtxt<'a> {
841841
pub fn span_err<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
842842
self.parse_sess.span_diagnostic.span_err(sp, msg);
843843
}
844+
pub fn span_err_with_code<S: Into<MultiSpan>>(&self, sp: S, msg: &str, code: DiagnosticId) {
845+
self.parse_sess.span_diagnostic.span_err_with_code(sp, msg, code);
846+
}
844847
pub fn mut_span_err<S: Into<MultiSpan>>(&self, sp: S, msg: &str)
845848
-> DiagnosticBuilder<'a> {
846849
self.parse_sess.span_diagnostic.mut_span_err(sp, msg)

src/libsyntax_ext/asm.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ impl State {
4545
}
4646
}
4747

48+
macro_rules! span_err_if_not_stage0 {
49+
($cx:expr, $sp:expr, $code:ident, $text:tt) => {
50+
#[cfg(not(stage0))] {
51+
span_err!($cx, $sp, $code, $text)
52+
}
53+
#[cfg(stage0)] {
54+
$cx.span_err($sp, $text)
55+
}
56+
}
57+
}
58+
4859
const OPTIONS: &'static [&'static str] = &["volatile", "alignstack", "intel"];
4960

5061
pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt,
@@ -89,7 +100,7 @@ pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt,
89100
if asm_str_style.is_some() {
90101
// If we already have a string with instructions,
91102
// ending up in Asm state again is an error.
92-
cx.span_err(sp, "malformed inline assembly");
103+
span_err_if_not_stage0!(cx, sp, E0660, "malformed inline assembly");
93104
return DummyResult::expr(sp);
94105
}
95106
// Nested parser, stop before the first colon (see above).
@@ -142,7 +153,8 @@ pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt,
142153
Some(Symbol::intern(&format!("={}", ch.as_str())))
143154
}
144155
_ => {
145-
cx.span_err(span, "output operand constraint lacks '=' or '+'");
156+
span_err_if_not_stage0!(cx, span, E0661,
157+
"output operand constraint lacks '=' or '+'");
146158
None
147159
}
148160
};

src/libsyntax_ext/diagnostics.rs

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2018 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+
#![allow(non_snake_case)]
12+
13+
// Error messages for EXXXX errors.
14+
// Each message should start and end with a new line, and be wrapped to 80 characters.
15+
// In vim you can `:set tw=80` and use `gq` to wrap paragraphs. Use `:set tw=0` to disable.
16+
register_long_diagnostics! {
17+
E0660: r##"
18+
The argument to the `asm` macro is not well-formed.
19+
20+
Erroneous code example:
21+
22+
```compile_fail,E0660
23+
asm!("nop" "nop");
24+
```
25+
26+
Considering that this would be a long explanation, we instead recommend you to
27+
take a look at the unstable book:
28+
https://doc.rust-lang.org/unstable-book/language-features/asm.html
29+
"##,
30+
31+
E0661: r##"
32+
An invalid syntax was passed to the second argument of an `asm` macro line.
33+
34+
Erroneous code example:
35+
36+
```compile_fail,E0661
37+
let a;
38+
asm!("nop" : "r"(a));
39+
```
40+
41+
Considering that this would be a long explanation, we instead recommend you to
42+
take a look at the unstable book:
43+
https://doc.rust-lang.org/unstable-book/language-features/asm.html
44+
"##,
45+
}

src/libsyntax_ext/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#![feature(decl_macro)]
1919
#![feature(str_escape)]
2020

21+
#![cfg_attr(not(stage0), feature(rustc_diagnostic_macros))]
22+
2123
extern crate fmt_macros;
2224
#[macro_use]
2325
extern crate syntax;
@@ -26,6 +28,9 @@ extern crate proc_macro;
2628
extern crate rustc_data_structures;
2729
extern crate rustc_errors as errors;
2830

31+
#[cfg(not(stage0))]
32+
mod diagnostics;
33+
2934
mod assert;
3035
mod asm;
3136
mod cfg;

src/test/compile-fail/issue-21045.rs renamed to src/test/ui/E0660.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@
77
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
10+
11+
// ignore-stage1
12+
1013
#![feature(asm)]
1114

1215
fn main() {
1316
let a;
14-
asm!("nop" "nop"); //~ ERROR malformed inline assembly
15-
asm!("nop" "nop" : "=r"(a)); //~ ERROR malformed inline assembly
17+
asm!("nop" "nop");
18+
//~^ ERROR E0660
19+
asm!("nop" "nop" : "=r"(a));
20+
//~^ ERROR E0660
1621
}

src/test/ui/E0660.stderr

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0660]: malformed inline assembly
2+
--> $DIR/E0660.rs:17:5
3+
|
4+
LL | asm!("nop" "nop");
5+
| ^^^^^^^^^^^^^^^^^^
6+
7+
error[E0660]: malformed inline assembly
8+
--> $DIR/E0660.rs:19:5
9+
|
10+
LL | asm!("nop" "nop" : "=r"(a));
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
13+
error: aborting due to 2 previous errors
14+
15+
For more information about this error, try `rustc --explain E0660`.

src/test/ui/E0661.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2018 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+
// ignore-stage1
12+
13+
#![feature(asm)]
14+
15+
fn main() {
16+
let a;
17+
asm!("nop" : "r"(a));
18+
//~^ ERROR E0661
19+
}

src/test/ui/E0661.stderr

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0661]: output operand constraint lacks '=' or '+'
2+
--> $DIR/E0661.rs:17:18
3+
|
4+
LL | asm!("nop" : "r"(a));
5+
| ^^^
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0661`.

0 commit comments

Comments
 (0)