@@ -149,15 +149,13 @@ pub(crate) struct OnUnknownData {
149149
150150impl OnUnknownData {
151151 pub ( crate ) fn from_attrs < ' tcx > ( tcx : TyCtxt < ' tcx > , item : & Item ) -> Option < OnUnknownData > {
152- if let Some ( Attribute :: Parsed ( AttributeKind :: OnUnknown { directive, .. } ) ) =
153- AttributeParser :: parse_limited (
154- tcx. sess ,
155- & item. attrs ,
156- & [ sym:: diagnostic, sym:: on_unknown] ,
157- item. span ,
158- item. id ,
159- Some ( tcx. features ( ) ) ,
160- )
152+ if tcx. features ( ) . diagnostic_on_unknown ( )
153+ && let Some ( Attribute :: Parsed ( AttributeKind :: OnUnknown { directive, .. } ) ) =
154+ AttributeParser :: parse_limited (
155+ tcx. sess ,
156+ & item. attrs ,
157+ & [ sym:: diagnostic, sym:: on_unknown] ,
158+ )
161159 {
162160 Some ( Self { directive : Box :: new ( * directive?) } )
163161 } else {
@@ -216,6 +214,8 @@ pub(crate) struct ImportData<'ra> {
216214 /// A `#[diagnostic::on_unknown]` attribute applied
217215 /// to the given import. This allows crates to specify
218216 /// custom error messages for a specific import
217+ ///
218+ /// This is `None` if the feature flag for `diagnostic::on_unknown` is disabled.
219219 pub on_unknown_attr : Option < OnUnknownData > ,
220220}
221221
@@ -845,24 +845,24 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
845845 . collect :: < Vec < _ > > ( ) ;
846846 let default_message =
847847 format ! ( "unresolved import{} {}" , pluralize!( paths. len( ) ) , paths. join( ", " ) , ) ;
848- let ( message, label, notes) = if self . tcx . features ( ) . diagnostic_on_unknown ( )
849- && let Some ( directive ) = errors [ 0 ] . 1 . on_unknown_attr . as_ref ( ) . map ( |a| & a . directive )
850- {
851- let args = FormatArgs {
852- this : paths. join ( ", " ) ,
853- // Unused
854- this_sugared : String :: new ( ) ,
855- // Unused
856- item_context : "" ,
857- // Unused
858- generic_args : Vec :: new ( ) ,
859- } ;
860- let CustomDiagnostic { message, label, notes, .. } = directive. eval ( None , & args) ;
848+ let ( message, label, notes) =
849+ // Feature gating for ` on_unknown_attr` happens initialization of the field
850+ if let Some ( directive ) = errors [ 0 ] . 1 . on_unknown_attr . as_ref ( ) . map ( |a| & a . directive ) {
851+ let args = FormatArgs {
852+ this : paths. join ( ", " ) ,
853+ // Unused
854+ this_sugared : String :: new ( ) ,
855+ // Unused
856+ item_context : "" ,
857+ // Unused
858+ generic_args : Vec :: new ( ) ,
859+ } ;
860+ let CustomDiagnostic { message, label, notes, .. } = directive. eval ( None , & args) ;
861861
862- ( message, label, notes)
863- } else {
864- ( None , None , Vec :: new ( ) )
865- } ;
862+ ( message, label, notes)
863+ } else {
864+ ( None , None , Vec :: new ( ) )
865+ } ;
866866 let has_custom_message = message. is_some ( ) ;
867867 let message = message. as_deref ( ) . unwrap_or ( default_message. as_str ( ) ) ;
868868
0 commit comments