@@ -6,14 +6,15 @@ use crate::check::callee::{self, DeferredCallResolution};
6
6
use crate :: check:: method:: { self , MethodCallee , SelfSource } ;
7
7
use crate :: check:: { BreakableCtxt , Diverges , Expectation , FallbackMode , FnCtxt , LocalTy } ;
8
8
9
+ use rustc_ast:: TraitObjectSyntax ;
9
10
use rustc_data_structures:: captures:: Captures ;
10
11
use rustc_data_structures:: fx:: FxHashSet ;
11
12
use rustc_errors:: { Applicability , DiagnosticBuilder , ErrorReported } ;
12
13
use rustc_hir as hir;
13
14
use rustc_hir:: def:: { CtorOf , DefKind , Res } ;
14
15
use rustc_hir:: def_id:: DefId ;
15
16
use rustc_hir:: lang_items:: LangItem ;
16
- use rustc_hir:: { ExprKind , GenericArg , Node , QPath } ;
17
+ use rustc_hir:: { ExprKind , GenericArg , Node , QPath , TyKind } ;
17
18
use rustc_infer:: infer:: canonical:: { Canonical , OriginalQueryValues , QueryResponse } ;
18
19
use rustc_infer:: infer:: error_reporting:: TypeAnnotationNeeded :: E0282 ;
19
20
use rustc_infer:: infer:: { InferOk , InferResult } ;
@@ -26,7 +27,9 @@ use rustc_middle::ty::{
26
27
self , AdtKind , CanonicalUserType , DefIdTree , GenericParamDefKind , ToPolyTraitRef , ToPredicate ,
27
28
Ty , UserType ,
28
29
} ;
29
- use rustc_session:: { lint, parse:: feature_err} ;
30
+ use rustc_session:: lint;
31
+ use rustc_session:: lint:: builtin:: BARE_TRAIT_OBJECTS ;
32
+ use rustc_session:: parse:: feature_err;
30
33
use rustc_span:: source_map:: { original_sp, DUMMY_SP } ;
31
34
use rustc_span:: symbol:: { kw, sym, Ident } ;
32
35
use rustc_span:: { self , BytePos , MultiSpan , Span } ;
@@ -472,7 +475,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
472
475
}
473
476
474
477
pub fn to_ty ( & self , ast_t : & hir:: Ty < ' _ > ) -> Ty < ' tcx > {
475
- let t = AstConv :: ast_ty_to_ty ( self , ast_t) ;
478
+ let t = < dyn AstConv < ' _ > > :: ast_ty_to_ty ( self , ast_t) ;
476
479
self . register_wf_obligation ( t. into ( ) , ast_t. span , traits:: MiscObligation ) ;
477
480
t
478
481
}
@@ -854,7 +857,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
854
857
// out unconstrained or ambiguous, as we're
855
858
// just trying to get hints here.
856
859
self . save_and_restore_in_snapshot_flag ( |_| {
857
- let mut fulfill = TraitEngine :: new ( self . tcx ) ;
860
+ let mut fulfill = < dyn TraitEngine < ' _ > > :: new ( self . tcx ) ;
858
861
for obligation in ok. obligations {
859
862
fulfill. register_predicate_obligation ( self , obligation) ;
860
863
}
@@ -947,6 +950,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
947
950
result
948
951
} ) ;
949
952
953
+ if result. is_ok ( ) {
954
+ self . maybe_lint_bare_trait ( qpath, hir_id) ;
955
+ }
956
+
950
957
// Write back the new resolution.
951
958
self . write_resolution ( hir_id, result) ;
952
959
(
@@ -956,6 +963,29 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
956
963
)
957
964
}
958
965
966
+ fn maybe_lint_bare_trait ( & self , qpath : & QPath < ' _ > , hir_id : hir:: HirId ) {
967
+ if let QPath :: TypeRelative ( self_ty, _) = qpath {
968
+ if let TyKind :: TraitObject ( [ poly_trait_ref, ..] , _, TraitObjectSyntax :: None ) =
969
+ self_ty. kind
970
+ {
971
+ self . tcx . struct_span_lint_hir ( BARE_TRAIT_OBJECTS , hir_id, self_ty. span , |lint| {
972
+ let mut db = lint
973
+ . build ( & format ! ( "trait objects without an explicit `dyn` are deprecated" ) ) ;
974
+ let ( sugg, app) = match self . tcx . sess . source_map ( ) . span_to_snippet ( self_ty. span )
975
+ {
976
+ Ok ( s) if poly_trait_ref. trait_ref . path . is_global ( ) => {
977
+ ( format ! ( "<dyn ({})>" , s) , Applicability :: MachineApplicable )
978
+ }
979
+ Ok ( s) => ( format ! ( "<dyn {}>" , s) , Applicability :: MachineApplicable ) ,
980
+ Err ( _) => ( "<dyn <type>>" . to_string ( ) , Applicability :: HasPlaceholders ) ,
981
+ } ;
982
+ db. span_suggestion ( self_ty. span , "use `dyn`" , sugg, app) ;
983
+ db. emit ( )
984
+ } ) ;
985
+ }
986
+ }
987
+ }
988
+
959
989
/// Given a function `Node`, return its `FnDecl` if it exists, or `None` otherwise.
960
990
pub ( in super :: super ) fn get_node_fn_decl (
961
991
& self ,
@@ -1174,9 +1204,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1174
1204
1175
1205
let path_segs = match res {
1176
1206
Res :: Local ( _) | Res :: SelfCtor ( _) => vec ! [ ] ,
1177
- Res :: Def ( kind, def_id) => {
1178
- AstConv :: def_ids_for_value_path_segments ( self , segments, self_ty, kind, def_id)
1179
- }
1207
+ Res :: Def ( kind, def_id) => < dyn AstConv < ' _ > > :: def_ids_for_value_path_segments (
1208
+ self , segments, self_ty, kind, def_id,
1209
+ ) ,
1180
1210
_ => bug ! ( "instantiate_value_path on {:?}" , res) ,
1181
1211
} ;
1182
1212
@@ -1219,7 +1249,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1219
1249
// errors if type parameters are provided in an inappropriate place.
1220
1250
1221
1251
let generic_segs: FxHashSet < _ > = path_segs. iter ( ) . map ( |PathSeg ( _, index) | index) . collect ( ) ;
1222
- let generics_has_err = AstConv :: prohibit_generics (
1252
+ let generics_has_err = < dyn AstConv < ' _ > > :: prohibit_generics (
1223
1253
self ,
1224
1254
segments. iter ( ) . enumerate ( ) . filter_map ( |( index, seg) | {
1225
1255
if !generic_segs. contains ( & index) || is_alias_variant_ctor {
@@ -1262,7 +1292,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1262
1292
if let GenericArgCountResult {
1263
1293
correct : Err ( GenericArgCountMismatch { reported : Some ( _) , .. } ) ,
1264
1294
..
1265
- } = AstConv :: check_generic_arg_count_for_call (
1295
+ } = < dyn AstConv < ' _ > > :: check_generic_arg_count_for_call (
1266
1296
tcx,
1267
1297
span,
1268
1298
def_id,
@@ -1370,7 +1400,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1370
1400
) -> subst:: GenericArg < ' tcx > {
1371
1401
match ( & param. kind , arg) {
1372
1402
( GenericParamDefKind :: Lifetime , GenericArg :: Lifetime ( lt) ) => {
1373
- AstConv :: ast_region_to_region ( self . fcx , lt, Some ( param) ) . into ( )
1403
+ < dyn AstConv < ' _ > > :: ast_region_to_region ( self . fcx , lt, Some ( param) ) . into ( )
1374
1404
}
1375
1405
( GenericParamDefKind :: Type { .. } , GenericArg :: Type ( ty) ) => {
1376
1406
self . fcx . to_ty ( ty) . into ( )
@@ -1423,7 +1453,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1423
1453
}
1424
1454
1425
1455
let substs = self_ctor_substs. unwrap_or_else ( || {
1426
- AstConv :: create_substs_for_generic_args (
1456
+ < dyn AstConv < ' _ > > :: create_substs_for_generic_args (
1427
1457
tcx,
1428
1458
def_id,
1429
1459
& [ ] [ ..] ,
0 commit comments