@@ -147,7 +147,7 @@ impl<'tcx> Visitor<'tcx> for CheckParameters<'tcx> {
147
147
fn check_asm < ' tcx > ( tcx : TyCtxt < ' tcx > , body : & ' tcx hir:: Body < ' tcx > , fn_span : Span ) {
148
148
let mut this = CheckInlineAssembly { tcx, items : Vec :: new ( ) } ;
149
149
this. visit_body ( body) ;
150
- if let [ ( ItemKind :: Asm , _) ] = this. items [ ..] {
150
+ if let [ ( ItemKind :: Asm | ItemKind :: Err , _) ] = this. items [ ..] {
151
151
// Ok.
152
152
} else {
153
153
let mut diag = struct_span_err ! (
@@ -156,19 +156,33 @@ fn check_asm<'tcx>(tcx: TyCtxt<'tcx>, body: &'tcx hir::Body<'tcx>, fn_span: Span
156
156
E0787 ,
157
157
"naked functions must contain a single asm block"
158
158
) ;
159
+
160
+ let mut must_show_error = false ;
159
161
let mut has_asm = false ;
162
+ let mut has_err = false ;
160
163
for & ( kind, span) in & this. items {
161
164
match kind {
162
165
ItemKind :: Asm if has_asm => {
166
+ must_show_error = true ;
163
167
diag. span_label ( span, "multiple asm blocks are unsupported in naked functions" ) ;
164
168
}
165
169
ItemKind :: Asm => has_asm = true ,
166
170
ItemKind :: NonAsm => {
171
+ must_show_error = true ;
167
172
diag. span_label ( span, "non-asm is unsupported in naked functions" ) ;
168
173
}
174
+ ItemKind :: Err => has_err = true ,
169
175
}
170
176
}
171
- diag. emit ( ) ;
177
+
178
+ // If the naked function only contains a single asm block and a non-zero number of
179
+ // errors, then don't show an additional error. This allows for appending/prepending
180
+ // `compile_error!("...")` statements and reduces error noise.
181
+ if must_show_error || !has_err {
182
+ diag. emit ( ) ;
183
+ } else {
184
+ diag. cancel ( ) ;
185
+ }
172
186
}
173
187
}
174
188
@@ -181,6 +195,7 @@ struct CheckInlineAssembly<'tcx> {
181
195
enum ItemKind {
182
196
Asm ,
183
197
NonAsm ,
198
+ Err ,
184
199
}
185
200
186
201
impl < ' tcx > CheckInlineAssembly < ' tcx > {
@@ -222,9 +237,13 @@ impl<'tcx> CheckInlineAssembly<'tcx> {
222
237
self . check_inline_asm ( asm, span) ;
223
238
}
224
239
225
- ExprKind :: DropTemps ( ..) | ExprKind :: Block ( ..) | ExprKind :: Err => {
240
+ ExprKind :: DropTemps ( ..) | ExprKind :: Block ( ..) => {
226
241
hir:: intravisit:: walk_expr ( self , expr) ;
227
242
}
243
+
244
+ ExprKind :: Err => {
245
+ self . items . push ( ( ItemKind :: Err , span) ) ;
246
+ }
228
247
}
229
248
}
230
249
0 commit comments