@@ -10,19 +10,19 @@ use rustc_const_eval::interpret::{
1010 ImmTy , InterpCx , InterpResult , Projectable , Scalar , format_interp_error, interp_ok,
1111} ;
1212use rustc_data_structures:: fx:: FxHashSet ;
13- use rustc_hir:: HirId ;
1413use rustc_hir:: def:: DefKind ;
14+ use rustc_hir:: { HirId , find_attr} ;
1515use rustc_index:: IndexVec ;
1616use rustc_index:: bit_set:: DenseBitSet ;
1717use rustc_middle:: bug;
1818use rustc_middle:: mir:: visit:: { MutatingUseContext , NonMutatingUseContext , PlaceContext , Visitor } ;
1919use rustc_middle:: mir:: * ;
2020use rustc_middle:: ty:: layout:: { LayoutError , LayoutOf , LayoutOfHelpers , TyAndLayout } ;
21- use rustc_middle:: ty:: { self , ConstInt , ScalarInt , Ty , TyCtxt , TypeVisitableExt } ;
21+ use rustc_middle:: ty:: { self , ConstInt , GenericArgKind , ScalarInt , Ty , TyCtxt , TypeVisitableExt } ;
2222use rustc_span:: Span ;
2323use tracing:: { debug, instrument, trace} ;
2424
25- use crate :: errors:: { AssertLint , AssertLintKind } ;
25+ use crate :: errors:: { AssertLint , AssertLintKind , AssertLintMessage } ;
2626
2727pub ( super ) struct KnownPanicsLint ;
2828
@@ -288,6 +288,25 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
288288 }
289289 }
290290
291+ fn report_assert_message (
292+ & self ,
293+ location : Location ,
294+ lint_kind : AssertLintKind ,
295+ msg : & ' static str ,
296+ ) {
297+ let source_info = self . body . source_info ( location) ;
298+ if let Some ( lint_root) = self . lint_root ( * source_info) {
299+ let span = source_info. span ;
300+ let message = AssertLintMessage :: < ( ) > :: Message ( msg) ;
301+ self . tcx . emit_node_span_lint (
302+ lint_kind. lint ( ) ,
303+ lint_root,
304+ span,
305+ AssertLint { span, message, lint_kind } ,
306+ ) ;
307+ }
308+ }
309+
291310 fn report_assert_as_lint (
292311 & self ,
293312 location : Location ,
@@ -297,11 +316,12 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
297316 let source_info = self . body . source_info ( location) ;
298317 if let Some ( lint_root) = self . lint_root ( * source_info) {
299318 let span = source_info. span ;
319+ let message = AssertLintMessage :: AssertKind ( assert_kind) ;
300320 self . tcx . emit_node_span_lint (
301321 lint_kind. lint ( ) ,
302322 lint_root,
303323 span,
304- AssertLint { span, assert_kind , lint_kind } ,
324+ AssertLint { span, message , lint_kind } ,
305325 ) ;
306326 }
307327 }
@@ -765,6 +785,22 @@ impl<'tcx> Visitor<'tcx> for ConstPropagator<'_, 'tcx> {
765785 }
766786 // We failed to evaluate the discriminant, fallback to visiting all successors.
767787 }
788+ TerminatorKind :: Call { func, args : _, .. } => {
789+ if let Some ( ( def_id, generic_args) ) = func. const_fn_def ( ) {
790+ for arg in generic_args {
791+ if let GenericArgKind :: Const ( ct) = arg. kind ( )
792+ && find_attr ! ( self . tcx, def_id, RustcPanicsWhenNIsZero )
793+ && let Some ( 0 ) = ct. try_to_target_usize ( self . tcx )
794+ {
795+ self . report_assert_message (
796+ location,
797+ AssertLintKind :: UnconditionalPanic ,
798+ "const parameter `N` is zero" ,
799+ ) ;
800+ }
801+ }
802+ }
803+ }
768804 // None of these have Operands to const-propagate.
769805 TerminatorKind :: Goto { .. }
770806 | TerminatorKind :: UnwindResume
@@ -777,7 +813,6 @@ impl<'tcx> Visitor<'tcx> for ConstPropagator<'_, 'tcx> {
777813 | TerminatorKind :: CoroutineDrop
778814 | TerminatorKind :: FalseEdge { .. }
779815 | TerminatorKind :: FalseUnwind { .. }
780- | TerminatorKind :: Call { .. }
781816 | TerminatorKind :: InlineAsm { .. } => { }
782817 }
783818
0 commit comments