@@ -147,7 +147,7 @@ impl<'tcx> Visitor<'tcx> for CheckParameters<'tcx> {
147147fn check_asm < ' tcx > ( tcx : TyCtxt < ' tcx > , body : & ' tcx hir:: Body < ' tcx > , fn_span : Span ) {
148148 let mut this = CheckInlineAssembly { tcx, items : Vec :: new ( ) } ;
149149 this. visit_body ( body) ;
150- if let [ ( ItemKind :: Asm , _) ] = this. items [ ..] {
150+ if let [ ( ItemKind :: Asm | ItemKind :: Err , _) ] = this. items [ ..] {
151151 // Ok.
152152 } else {
153153 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
156156 E0787 ,
157157 "naked functions must contain a single asm block"
158158 ) ;
159+
160+ let mut must_show_error = false ;
159161 let mut has_asm = false ;
162+ let mut has_err = false ;
160163 for & ( kind, span) in & this. items {
161164 match kind {
162165 ItemKind :: Asm if has_asm => {
166+ must_show_error = true ;
163167 diag. span_label ( span, "multiple asm blocks are unsupported in naked functions" ) ;
164168 }
165169 ItemKind :: Asm => has_asm = true ,
166170 ItemKind :: NonAsm => {
171+ must_show_error = true ;
167172 diag. span_label ( span, "non-asm is unsupported in naked functions" ) ;
168173 }
174+ ItemKind :: Err => has_err = true ,
169175 }
170176 }
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+ }
172186 }
173187}
174188
@@ -181,6 +195,7 @@ struct CheckInlineAssembly<'tcx> {
181195enum ItemKind {
182196 Asm ,
183197 NonAsm ,
198+ Err ,
184199}
185200
186201impl < ' tcx > CheckInlineAssembly < ' tcx > {
@@ -222,9 +237,13 @@ impl<'tcx> CheckInlineAssembly<'tcx> {
222237 self . check_inline_asm ( asm, span) ;
223238 }
224239
225- ExprKind :: DropTemps ( ..) | ExprKind :: Block ( ..) | ExprKind :: Err => {
240+ ExprKind :: DropTemps ( ..) | ExprKind :: Block ( ..) => {
226241 hir:: intravisit:: walk_expr ( self , expr) ;
227242 }
243+
244+ ExprKind :: Err => {
245+ self . items . push ( ( ItemKind :: Err , span) ) ;
246+ }
228247 }
229248 }
230249
0 commit comments