@@ -24,8 +24,11 @@ use rustc_trait_selection::traits::SelectionContext;
2424
2525use super :: ConstCx ;
2626use crate :: errors:: {
27- MutDerefErr , NonConstOpErr , PanicNonStrErr , RawPtrToIntErr , StaticAccessErr ,
28- TransientMutBorrowErr , TransientMutBorrowErrRaw ,
27+ InteriorMutabilityBorrow , InteriorMutableDataRefer , MutDerefErr , NonConstFmtMacroCall ,
28+ NonConstFnCall , NonConstOpErr , PanicNonStrErr , RawPtrToIntErr , StaticAccessErr ,
29+ TransientMutBorrowErr , TransientMutBorrowErrRaw , UnallowedFnPointerCall ,
30+ UnallowedHeapAllocations , UnallowedInlineAsm , UnallowedMutableRefs , UnallowedMutableRefsRaw ,
31+ UnallowedOpInConstContext , UnstableConstFn ,
2932} ;
3033use crate :: util:: { call_kind, CallDesugaringKind , CallKind } ;
3134
@@ -97,10 +100,7 @@ impl<'tcx> NonConstOp<'tcx> for FnCallIndirect {
97100 ccx : & ConstCx < ' _ , ' tcx > ,
98101 span : Span ,
99102 ) -> DiagnosticBuilder < ' tcx , ErrorGuaranteed > {
100- ccx. tcx . sess . struct_span_err (
101- span,
102- & format ! ( "function pointer calls are not allowed in {}s" , ccx. const_kind( ) ) ,
103- )
103+ ccx. tcx . sess . create_err ( UnallowedFnPointerCall { span, kind : ccx. const_kind ( ) } )
104104 }
105105}
106106
@@ -308,22 +308,13 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
308308 err
309309 }
310310 _ if tcx. opt_parent ( callee) == tcx. get_diagnostic_item ( sym:: ArgumentV1Methods ) => {
311- struct_span_err ! (
312- ccx. tcx. sess,
313- span,
314- E0015 ,
315- "cannot call non-const formatting macro in {}s" ,
316- ccx. const_kind( ) ,
317- )
311+ ccx. tcx . sess . create_err ( NonConstFmtMacroCall { span, kind : ccx. const_kind ( ) } )
318312 }
319- _ => struct_span_err ! (
320- ccx. tcx. sess,
313+ _ => ccx. tcx . sess . create_err ( NonConstFnCall {
321314 span,
322- E0015 ,
323- "cannot call non-const fn `{}` in {}s" ,
324- ccx. tcx. def_path_str_with_substs( callee, substs) ,
325- ccx. const_kind( ) ,
326- ) ,
315+ def_path_str : ccx. tcx . def_path_str_with_substs ( callee, substs) ,
316+ kind : ccx. const_kind ( ) ,
317+ } ) ,
327318 } ;
328319
329320 err. note ( & format ! (
@@ -354,10 +345,10 @@ impl<'tcx> NonConstOp<'tcx> for FnCallUnstable {
354345 ) -> DiagnosticBuilder < ' tcx , ErrorGuaranteed > {
355346 let FnCallUnstable ( def_id, feature) = * self ;
356347
357- let mut err = ccx. tcx . sess . struct_span_err (
358- span ,
359- & format ! ( "`{}` is not yet stable as a const fn" , ccx . tcx . def_path_str ( def_id ) ) ,
360- ) ;
348+ let mut err = ccx
349+ . tcx
350+ . sess
351+ . create_err ( UnstableConstFn { span , def_path : ccx . tcx . def_path_str ( def_id ) } ) ;
361352
362353 if ccx. is_const_stable_const_fn ( ) {
363354 err. help ( "const-stable functions can only call other const-stable functions" ) ;
@@ -392,9 +383,12 @@ impl<'tcx> NonConstOp<'tcx> for Generator {
392383 ) -> DiagnosticBuilder < ' tcx , ErrorGuaranteed > {
393384 let msg = format ! ( "{}s are not allowed in {}s" , self . 0 , ccx. const_kind( ) ) ;
394385 if let hir:: GeneratorKind :: Async ( hir:: AsyncGeneratorKind :: Block ) = self . 0 {
395- feature_err ( & ccx. tcx . sess . parse_sess , sym:: const_async_blocks, span, & msg)
386+ ccx. tcx . sess . create_feature_err (
387+ UnallowedOpInConstContext { span, msg } ,
388+ sym:: const_async_blocks,
389+ )
396390 } else {
397- ccx. tcx . sess . struct_span_err ( span, & msg)
391+ ccx. tcx . sess . create_err ( UnallowedOpInConstContext { span, msg } )
398392 }
399393 }
400394}
@@ -407,23 +401,11 @@ impl<'tcx> NonConstOp<'tcx> for HeapAllocation {
407401 ccx : & ConstCx < ' _ , ' tcx > ,
408402 span : Span ,
409403 ) -> DiagnosticBuilder < ' tcx , ErrorGuaranteed > {
410- let mut err = struct_span_err ! (
411- ccx. tcx. sess,
404+ ccx. tcx . sess . create_err ( UnallowedHeapAllocations {
412405 span,
413- E0010 ,
414- "allocations are not allowed in {}s" ,
415- ccx. const_kind( )
416- ) ;
417- err. span_label ( span, format ! ( "allocation not allowed in {}s" , ccx. const_kind( ) ) ) ;
418- if ccx. tcx . sess . teach ( & err. get_code ( ) . unwrap ( ) ) {
419- err. note (
420- "The value of statics and constants must be known at compile time, \
421- and they live for the entire lifetime of a program. Creating a boxed \
422- value allocates memory on the heap at runtime, and therefore cannot \
423- be done at compile time.",
424- ) ;
425- }
426- err
406+ kind : ccx. const_kind ( ) ,
407+ teach : ccx. tcx . sess . teach ( & error_code ! ( E0010 ) ) . then_some ( ( ) ) ,
408+ } )
427409 }
428410}
429411
@@ -435,13 +417,7 @@ impl<'tcx> NonConstOp<'tcx> for InlineAsm {
435417 ccx : & ConstCx < ' _ , ' tcx > ,
436418 span : Span ,
437419 ) -> DiagnosticBuilder < ' tcx , ErrorGuaranteed > {
438- struct_span_err ! (
439- ccx. tcx. sess,
440- span,
441- E0015 ,
442- "inline assembly is not allowed in {}s" ,
443- ccx. const_kind( )
444- )
420+ ccx. tcx . sess . create_err ( UnallowedInlineAsm { span, kind : ccx. const_kind ( ) } )
445421 }
446422}
447423
@@ -487,12 +463,7 @@ impl<'tcx> NonConstOp<'tcx> for TransientCellBorrow {
487463 ccx : & ConstCx < ' _ , ' tcx > ,
488464 span : Span ,
489465 ) -> DiagnosticBuilder < ' tcx , ErrorGuaranteed > {
490- feature_err (
491- & ccx. tcx . sess . parse_sess ,
492- sym:: const_refs_to_cell,
493- span,
494- "cannot borrow here, since the borrowed element may contain interior mutability" ,
495- )
466+ ccx. tcx . sess . create_feature_err ( InteriorMutabilityBorrow { span } , sym:: const_refs_to_cell)
496467 }
497468}
498469
@@ -507,32 +478,22 @@ impl<'tcx> NonConstOp<'tcx> for CellBorrow {
507478 ccx : & ConstCx < ' _ , ' tcx > ,
508479 span : Span ,
509480 ) -> DiagnosticBuilder < ' tcx , ErrorGuaranteed > {
510- let mut err = struct_span_err ! (
511- ccx. tcx. sess,
512- span,
513- E0492 ,
514- "{}s cannot refer to interior mutable data" ,
515- ccx. const_kind( ) ,
516- ) ;
517- err. span_label (
518- span,
519- "this borrow of an interior mutable value may end up in the final value" ,
520- ) ;
481+ // FIXME: Maybe a more elegant solution to this if else case
521482 if let hir:: ConstContext :: Static ( _) = ccx. const_kind ( ) {
522- err. help (
523- "to fix this, the value can be extracted to a separate \
524- `static` item and then referenced",
525- ) ;
526- }
527- if ccx. tcx . sess . teach ( & err. get_code ( ) . unwrap ( ) ) {
528- err. note (
529- "A constant containing interior mutable data behind a reference can allow you
530- to modify that data. This would make multiple uses of a constant to be able to
531- see different values and allow circumventing the `Send` and `Sync` requirements
532- for shared mutable data, which is unsound." ,
533- ) ;
483+ ccx. tcx . sess . create_err ( InteriorMutableDataRefer {
484+ span,
485+ opt_help : Some ( ( ) ) ,
486+ kind : ccx. const_kind ( ) ,
487+ teach : ccx. tcx . sess . teach ( & error_code ! ( E0492 ) ) . then_some ( ( ) ) ,
488+ } )
489+ } else {
490+ ccx. tcx . sess . create_err ( InteriorMutableDataRefer {
491+ span,
492+ opt_help : None ,
493+ kind : ccx. const_kind ( ) ,
494+ teach : ccx. tcx . sess . teach ( & error_code ! ( E0492 ) ) . then_some ( ( ) ) ,
495+ } )
534496 }
535- err
536497 }
537498}
538499
@@ -558,33 +519,18 @@ impl<'tcx> NonConstOp<'tcx> for MutBorrow {
558519 ccx : & ConstCx < ' _ , ' tcx > ,
559520 span : Span ,
560521 ) -> DiagnosticBuilder < ' tcx , ErrorGuaranteed > {
561- let raw = match self . 0 {
562- hir:: BorrowKind :: Raw => "raw " ,
563- hir:: BorrowKind :: Ref => "" ,
564- } ;
565-
566- let mut err = struct_span_err ! (
567- ccx. tcx. sess,
568- span,
569- E0764 ,
570- "{}mutable references are not allowed in the final value of {}s" ,
571- raw,
572- ccx. const_kind( ) ,
573- ) ;
574-
575- if ccx. tcx . sess . teach ( & err. get_code ( ) . unwrap ( ) ) {
576- err. note (
577- "References in statics and constants may only refer \
578- to immutable values.\n \n \
579- Statics are shared everywhere, and if they refer to \
580- mutable data one might violate memory safety since \
581- holding multiple mutable references to shared data \
582- is not allowed.\n \n \
583- If you really want global mutable state, try using \
584- static mut or a global UnsafeCell.",
585- ) ;
522+ match self . 0 {
523+ hir:: BorrowKind :: Raw => ccx. tcx . sess . create_err ( UnallowedMutableRefsRaw {
524+ span,
525+ kind : ccx. const_kind ( ) ,
526+ teach : ccx. tcx . sess . teach ( & error_code ! ( E0764 ) ) . then_some ( ( ) ) ,
527+ } ) ,
528+ hir:: BorrowKind :: Ref => ccx. tcx . sess . create_err ( UnallowedMutableRefs {
529+ span,
530+ kind : ccx. const_kind ( ) ,
531+ teach : ccx. tcx . sess . teach ( & error_code ! ( E0764 ) ) . then_some ( ( ) ) ,
532+ } ) ,
586533 }
587- err
588534 }
589535}
590536
0 commit comments