@@ -14,8 +14,13 @@ use crate::check::Expectation::{self, ExpectCastableToType, ExpectHasType, NoExp
1414use crate :: check:: FnCtxt ;
1515use crate :: check:: Needs ;
1616use crate :: check:: TupleArgumentsFlag :: DontTupleArguments ;
17+ use crate :: errors:: {
18+ FieldMultiplySpecifiedInInitializer , FunctionalRecordUpdateOnNonStruct ,
19+ YieldExprOutsideOfGenerator ,
20+ } ;
1721use crate :: type_error_struct;
1822
23+ use crate :: errors:: { AddressOfTemporaryTaken , ReturnStmtOutsideOfFnBody , StructExprNonExhaustive } ;
1924use rustc_ast as ast;
2025use rustc_ast:: util:: lev_distance:: find_best_match_for_name;
2126use rustc_data_structures:: fx:: FxHashMap ;
@@ -439,14 +444,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
439444 } )
440445 } ) ;
441446 if !is_named {
442- struct_span_err ! (
443- self . tcx. sess,
444- oprnd. span,
445- E0745 ,
446- "cannot take address of a temporary"
447- )
448- . span_label ( oprnd. span , "temporary value" )
449- . emit ( ) ;
447+ self . tcx . sess . emit_err ( AddressOfTemporaryTaken { span : oprnd. span } )
450448 }
451449 }
452450
@@ -665,13 +663,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
665663 expr : & ' tcx hir:: Expr < ' tcx > ,
666664 ) -> Ty < ' tcx > {
667665 if self . ret_coercion . is_none ( ) {
668- struct_span_err ! (
669- self . tcx. sess,
670- expr. span,
671- E0572 ,
672- "return statement outside of function body" ,
673- )
674- . emit ( ) ;
666+ self . tcx . sess . emit_err ( ReturnStmtOutsideOfFnBody { span : expr. span } ) ;
675667 } else if let Some ( ref e) = expr_opt {
676668 if self . ret_coercion_span . borrow ( ) . is_none ( ) {
677669 * self . ret_coercion_span . borrow_mut ( ) = Some ( e. span ) ;
@@ -740,6 +732,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
740732 expr_span : & Span ,
741733 ) {
742734 if !lhs. is_syntactic_place_expr ( ) {
735+ // FIXME: Make this use SessionDiagnostic once error codes can be dynamically set.
743736 let mut err = self . tcx . sess . struct_span_err_with_code (
744737 * expr_span,
745738 "invalid left-hand side of assignment" ,
@@ -1120,14 +1113,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11201113 // Prohibit struct expressions when non-exhaustive flag is set.
11211114 let adt = adt_ty. ty_adt_def ( ) . expect ( "`check_struct_path` returned non-ADT type" ) ;
11221115 if !adt. did . is_local ( ) && variant. is_field_list_non_exhaustive ( ) {
1123- struct_span_err ! (
1124- self . tcx. sess,
1125- expr. span,
1126- E0639 ,
1127- "cannot create non-exhaustive {} using struct expression" ,
1128- adt. variant_descr( )
1129- )
1130- . emit ( ) ;
1116+ self . tcx
1117+ . sess
1118+ . emit_err ( StructExprNonExhaustive { span : expr. span , what : adt. variant_descr ( ) } ) ;
11311119 }
11321120
11331121 let error_happened = self . check_expr_struct_fields (
@@ -1165,13 +1153,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11651153 . insert ( expr. hir_id , fru_field_types) ;
11661154 }
11671155 _ => {
1168- struct_span_err ! (
1169- self . tcx. sess,
1170- base_expr. span,
1171- E0436 ,
1172- "functional record update syntax requires a struct"
1173- )
1174- . emit ( ) ;
1156+ self . tcx
1157+ . sess
1158+ . emit_err ( FunctionalRecordUpdateOnNonStruct { span : base_expr. span } ) ;
11751159 }
11761160 }
11771161 }
@@ -1234,18 +1218,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12341218 } else {
12351219 error_happened = true ;
12361220 if let Some ( prev_span) = seen_fields. get ( & ident) {
1237- let mut err = struct_span_err ! (
1238- self . tcx. sess,
1239- field. ident. span,
1240- E0062 ,
1241- "field `{}` specified more than once" ,
1242- ident
1243- ) ;
1244-
1245- err. span_label ( field. ident . span , "used more than once" ) ;
1246- err. span_label ( * prev_span, format ! ( "first use of `{}`" , ident) ) ;
1247-
1248- err. emit ( ) ;
1221+ tcx. sess . emit_err ( FieldMultiplySpecifiedInInitializer {
1222+ span : field. ident . span ,
1223+ prev_span : * prev_span,
1224+ ident,
1225+ } ) ;
12491226 } else {
12501227 self . report_unknown_field ( adt_ty, variant, field, ast_fields, kind_name, span) ;
12511228 }
@@ -1876,13 +1853,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
18761853 self . tcx . mk_unit ( )
18771854 }
18781855 _ => {
1879- struct_span_err ! (
1880- self . tcx. sess,
1881- expr. span,
1882- E0627 ,
1883- "yield expression outside of generator literal"
1884- )
1885- . emit ( ) ;
1856+ self . tcx . sess . emit_err ( YieldExprOutsideOfGenerator { span : expr. span } ) ;
18861857 self . tcx . mk_unit ( )
18871858 }
18881859 }
0 commit comments