@@ -31,7 +31,7 @@ use rustc_errors::{
3131} ;
3232use rustc_hir:: def:: { CtorKind , CtorOf , DefKind , Namespace , Res } ;
3333use rustc_hir:: def_id:: { DefId , LocalDefId } ;
34- use rustc_hir:: { self as hir, AnonConst , GenericArg , GenericArgs , HirId } ;
34+ use rustc_hir:: { self as hir, AnonConst , GenericArg , GenericArgs , HirId , LangItem } ;
3535use rustc_infer:: infer:: { InferCtxt , TyCtxtInferExt } ;
3636use rustc_infer:: traits:: ObligationCause ;
3737use rustc_middle:: middle:: stability:: AllowUnstable ;
@@ -46,6 +46,7 @@ use rustc_middle::{bug, span_bug};
4646use rustc_session:: lint:: builtin:: AMBIGUOUS_ASSOCIATED_ITEMS ;
4747use rustc_span:: edit_distance:: find_best_match_for_name;
4848use rustc_span:: { DUMMY_SP , Ident , Span , Symbol , kw} ;
49+ use rustc_trait_selection:: error_reporting:: InferCtxtErrorExt as _;
4950use rustc_trait_selection:: infer:: InferCtxtExt ;
5051use rustc_trait_selection:: traits:: wf:: object_region_bounds;
5152use rustc_trait_selection:: traits:: { self , ObligationCtxt } ;
@@ -2457,6 +2458,35 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
24572458 hir:: PatExprKind :: Lit { lit, negated } => {
24582459 let lit_input =
24592460 LitToConstInput { lit : & lit. node , ty, neg : negated } ;
2461+ let mut ty = ty;
2462+ if negated {
2463+ let infcx_;
2464+ let infcx = match self . infcx ( ) {
2465+ Some ( infcx) => infcx,
2466+ None => {
2467+ assert ! ( !ty. has_infer( ) ) ;
2468+ infcx_ = tcx
2469+ . infer_ctxt ( )
2470+ . ignoring_regions ( )
2471+ . build ( TypingMode :: non_body_analysis ( ) ) ;
2472+ & infcx_
2473+ }
2474+ } ;
2475+ let ocx = ObligationCtxt :: new_with_diagnostics ( infcx) ;
2476+ ocx. register_bound (
2477+ ObligationCause :: dummy_with_span ( expr. span ) ,
2478+ self . tcx ( ) . param_env ( self . item_def_id ( ) ) ,
2479+ ty,
2480+ self . tcx ( )
2481+ . require_lang_item ( LangItem :: Neg , Some ( expr. span ) ) ,
2482+ ) ;
2483+ let errors = ocx. select_all_or_error ( ) ;
2484+ if !errors. is_empty ( ) {
2485+ let guar =
2486+ infcx. err_ctxt ( ) . report_fulfillment_errors ( errors) ;
2487+ ty = Ty :: new_error ( tcx, guar) ;
2488+ }
2489+ }
24602490 let ct = tcx. lit_to_const ( lit_input) ;
24612491 ( ct, ty)
24622492 }
0 commit comments