@@ -35,6 +35,12 @@ use crate::errors;
3535type QualifResults < ' mir , ' tcx , Q > =
3636 rustc_mir_dataflow:: ResultsCursor < ' mir , ' tcx , FlowSensitiveAnalysis < ' mir , ' mir , ' tcx , Q > > ;
3737
38+ #[ derive( Copy , Clone , PartialEq , Eq , Debug ) ]
39+ enum ConstConditionsHold {
40+ Yes ,
41+ No ,
42+ }
43+
3844#[ derive( Default ) ]
3945pub ( crate ) struct Qualifs < ' mir , ' tcx > {
4046 has_mut_interior : Option < QualifResults < ' mir , ' tcx , HasMutInterior > > ,
@@ -376,15 +382,15 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {
376382 callee : DefId ,
377383 callee_args : ty:: GenericArgsRef < ' tcx > ,
378384 call_span : Span ,
379- ) -> bool {
385+ ) -> Option < ConstConditionsHold > {
380386 let tcx = self . tcx ;
381387 if !tcx. is_conditionally_const ( callee) {
382- return false ;
388+ return None ;
383389 }
384390
385391 let const_conditions = tcx. const_conditions ( callee) . instantiate ( tcx, callee_args) ;
386392 if const_conditions. is_empty ( ) {
387- return false ;
393+ return None ;
388394 }
389395
390396 let ( infcx, param_env) = tcx. infer_ctxt ( ) . build_with_typing_env ( self . body . typing_env ( tcx) ) ;
@@ -413,12 +419,13 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {
413419 } ) ) ;
414420
415421 let errors = ocx. select_all_or_error ( ) ;
416- if !errors. is_empty ( ) {
422+ if errors. is_empty ( ) {
423+ Some ( ConstConditionsHold :: Yes )
424+ } else {
417425 tcx. dcx ( )
418426 . span_delayed_bug ( call_span, "this should have reported a ~const error in HIR" ) ;
427+ Some ( ConstConditionsHold :: No )
419428 }
420-
421- true
422429 }
423430
424431 pub fn check_drop_terminator (
@@ -706,7 +713,10 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
706713 trace ! ( "attempting to call a trait method" ) ;
707714 let trait_is_const = tcx. is_const_trait ( trait_did) ;
708715
709- if trait_is_const {
716+ // Only consider a trait to be const if the const conditions hold.
717+ // Otherwise, it's really misleading to call something "conditionally"
718+ // const when it's very obviously not conditionally const.
719+ if trait_is_const && has_const_conditions == Some ( ConstConditionsHold :: Yes ) {
710720 // Trait calls are always conditionally-const.
711721 self . check_op ( ops:: ConditionallyConstCall {
712722 callee,
@@ -730,7 +740,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
730740 }
731741
732742 // Even if we know the callee, ensure we can use conditionally-const calls.
733- if has_const_conditions {
743+ if has_const_conditions. is_some ( ) {
734744 self . check_op ( ops:: ConditionallyConstCall {
735745 callee,
736746 args : fn_args,
0 commit comments