@@ -6,9 +6,8 @@ use rustc::session::Session;
66use rustc:: ty:: { self , DefIdTree } ;
77use rustc_ast_pretty:: pprust;
88use rustc_data_structures:: fx:: FxHashSet ;
9- use rustc_errors:: { pluralize , struct_span_err, Applicability , DiagnosticBuilder } ;
9+ use rustc_errors:: { struct_span_err, Applicability , DiagnosticBuilder } ;
1010use rustc_feature:: BUILTIN_ATTRIBUTES ;
11- use rustc_hir as hir;
1211use rustc_hir:: def:: Namespace :: { self , * } ;
1312use rustc_hir:: def:: { self , CtorKind , CtorOf , DefKind , NonMacroAttrKind } ;
1413use rustc_hir:: def_id:: { DefId , CRATE_DEF_INDEX , LOCAL_CRATE } ;
@@ -20,7 +19,6 @@ use syntax::ast::{self, Ident, Path};
2019use syntax:: util:: lev_distance:: find_best_match_for_name;
2120
2221use crate :: imports:: { ImportDirective , ImportDirectiveSubclass , ImportResolver } ;
23- use crate :: lifetimes:: { ElisionFailureInfo , LifetimeContext } ;
2422use crate :: path_names_to_string;
2523use crate :: { AmbiguityError , AmbiguityErrorMisc , AmbiguityKind } ;
2624use crate :: { BindingError , CrateLint , HasGenericParams , LegacyScope , Module , ModuleOrUniformRoot } ;
@@ -49,40 +47,6 @@ crate struct ImportSuggestion {
4947 pub path : Path ,
5048}
5149
52- crate enum MissingLifetimeSpot < ' tcx > {
53- Generics ( & ' tcx hir:: Generics < ' tcx > ) ,
54- HigherRanked { span : Span , span_type : ForLifetimeSpanType } ,
55- }
56-
57- crate enum ForLifetimeSpanType {
58- BoundEmpty ,
59- BoundTail ,
60- TypeEmpty ,
61- TypeTail ,
62- }
63-
64- impl ForLifetimeSpanType {
65- crate fn descr ( & self ) -> & ' static str {
66- match self {
67- Self :: BoundEmpty | Self :: BoundTail => "bound" ,
68- Self :: TypeEmpty | Self :: TypeTail => "type" ,
69- }
70- }
71-
72- crate fn suggestion ( & self , sugg : & str ) -> String {
73- match self {
74- Self :: BoundEmpty | Self :: TypeEmpty => format ! ( "for<{}> " , sugg) ,
75- Self :: BoundTail | Self :: TypeTail => format ! ( ", {}" , sugg) ,
76- }
77- }
78- }
79-
80- impl < ' tcx > Into < MissingLifetimeSpot < ' tcx > > for & ' tcx hir:: Generics < ' tcx > {
81- fn into ( self ) -> MissingLifetimeSpot < ' tcx > {
82- MissingLifetimeSpot :: Generics ( self )
83- }
84- }
85-
8650/// Adjust the impl span so that just the `impl` keyword is taken by removing
8751/// everything after `<` (`"impl<T> Iterator for A<T> {}" -> "impl"`) and
8852/// everything after the first whitespace (`"impl Iterator for A" -> "impl"`).
@@ -1491,208 +1455,3 @@ crate fn show_candidates(
14911455 err. note ( & msg) ;
14921456 }
14931457}
1494-
1495- impl < ' tcx > LifetimeContext < ' _ , ' tcx > {
1496- crate fn report_missing_lifetime_specifiers (
1497- & self ,
1498- span : Span ,
1499- count : usize ,
1500- ) -> DiagnosticBuilder < ' tcx > {
1501- struct_span_err ! (
1502- self . tcx. sess,
1503- span,
1504- E0106 ,
1505- "missing lifetime specifier{}" ,
1506- pluralize!( count)
1507- )
1508- }
1509-
1510- crate fn emit_undeclared_lifetime_error ( & self , lifetime_ref : & hir:: Lifetime ) {
1511- let mut err = struct_span_err ! (
1512- self . tcx. sess,
1513- lifetime_ref. span,
1514- E0261 ,
1515- "use of undeclared lifetime name `{}`" ,
1516- lifetime_ref
1517- ) ;
1518- err. span_label ( lifetime_ref. span , "undeclared lifetime" ) ;
1519- for missing in & self . missing_named_lifetime_spots {
1520- match missing {
1521- MissingLifetimeSpot :: Generics ( generics) => {
1522- let ( span, sugg) = if let Some ( param) = generics
1523- . params
1524- . iter ( )
1525- . filter ( |p| match p. kind {
1526- hir:: GenericParamKind :: Type {
1527- synthetic : Some ( hir:: SyntheticTyParamKind :: ImplTrait ) ,
1528- ..
1529- } => false ,
1530- _ => true ,
1531- } )
1532- . next ( )
1533- {
1534- ( param. span . shrink_to_lo ( ) , format ! ( "{}, " , lifetime_ref) )
1535- } else {
1536- ( generics. span , format ! ( "<{}>" , lifetime_ref) )
1537- } ;
1538- err. span_suggestion (
1539- span,
1540- & format ! ( "consider introducing lifetime `{}` here" , lifetime_ref) ,
1541- sugg,
1542- Applicability :: MaybeIncorrect ,
1543- ) ;
1544- }
1545- MissingLifetimeSpot :: HigherRanked { span, span_type } => {
1546- err. span_suggestion (
1547- * span,
1548- & format ! (
1549- "consider making the {} lifetime-generic with a new `{}` lifetime" ,
1550- span_type. descr( ) ,
1551- lifetime_ref
1552- ) ,
1553- span_type. suggestion ( & lifetime_ref. to_string ( ) ) ,
1554- Applicability :: MaybeIncorrect ,
1555- ) ;
1556- err. note (
1557- "for more information on higher-ranked polymorphism, visit \
1558- https://doc.rust-lang.org/nomicon/hrtb.html",
1559- ) ;
1560- }
1561- }
1562- }
1563- err. emit ( ) ;
1564- }
1565-
1566- crate fn is_trait_ref_fn_scope ( & mut self , trait_ref : & ' tcx hir:: PolyTraitRef < ' tcx > ) -> bool {
1567- if let def:: Res :: Def ( _, did) = trait_ref. trait_ref . path . res {
1568- if [
1569- self . tcx . lang_items ( ) . fn_once_trait ( ) ,
1570- self . tcx . lang_items ( ) . fn_trait ( ) ,
1571- self . tcx . lang_items ( ) . fn_mut_trait ( ) ,
1572- ]
1573- . contains ( & Some ( did) )
1574- {
1575- let ( span, span_type) = match & trait_ref. bound_generic_params {
1576- [ ] => ( trait_ref. span . shrink_to_lo ( ) , ForLifetimeSpanType :: BoundEmpty ) ,
1577- [ .., bound] => ( bound. span . shrink_to_hi ( ) , ForLifetimeSpanType :: BoundTail ) ,
1578- } ;
1579- self . missing_named_lifetime_spots
1580- . push ( MissingLifetimeSpot :: HigherRanked { span, span_type } ) ;
1581- return true ;
1582- }
1583- } ;
1584- false
1585- }
1586-
1587- crate fn add_missing_lifetime_specifiers_label (
1588- & self ,
1589- err : & mut DiagnosticBuilder < ' _ > ,
1590- span : Span ,
1591- count : usize ,
1592- lifetime_names : & FxHashSet < ast:: Ident > ,
1593- params : & [ ElisionFailureInfo ] ,
1594- ) {
1595- if count > 1 {
1596- err. span_label ( span, format ! ( "expected {} lifetime parameters" , count) ) ;
1597- } else {
1598- let snippet = self . tcx . sess . source_map ( ) . span_to_snippet ( span) . ok ( ) ;
1599- let suggest_existing = |err : & mut DiagnosticBuilder < ' _ > , sugg| {
1600- err. span_suggestion (
1601- span,
1602- "consider using the named lifetime" ,
1603- sugg,
1604- Applicability :: MaybeIncorrect ,
1605- ) ;
1606- } ;
1607- let suggest_new =
1608- |err : & mut DiagnosticBuilder < ' _ > , sugg : & str | {
1609- err. span_label ( span, "expected named lifetime parameter" ) ;
1610-
1611- for missing in self . missing_named_lifetime_spots . iter ( ) . rev ( ) {
1612- let mut introduce_suggestion = vec ! [ ] ;
1613- let msg;
1614- let should_break;
1615- introduce_suggestion. push ( match missing {
1616- MissingLifetimeSpot :: Generics ( generics) => {
1617- msg = "consider introducing a named lifetime parameter" . to_string ( ) ;
1618- should_break = true ;
1619- if let Some ( param) = generics. params . iter ( ) . filter ( |p| match p. kind {
1620- hir:: GenericParamKind :: Type {
1621- synthetic : Some ( hir:: SyntheticTyParamKind :: ImplTrait ) ,
1622- ..
1623- } => false ,
1624- _ => true ,
1625- } ) . next ( ) {
1626- ( param. span . shrink_to_lo ( ) , "'a, " . to_string ( ) )
1627- } else {
1628- ( generics. span , "<'a>" . to_string ( ) )
1629- }
1630- }
1631- MissingLifetimeSpot :: HigherRanked { span, span_type } => {
1632- msg = format ! (
1633- "consider making the {} lifetime-generic with a new `'a` lifetime" ,
1634- span_type. descr( ) ,
1635- ) ;
1636- should_break = false ;
1637- err. note (
1638- "for more information on higher-ranked polymorphism, visit \
1639- https://doc.rust-lang.org/nomicon/hrtb.html",
1640- ) ;
1641- ( * span, span_type. suggestion ( "'a" ) )
1642- }
1643- } ) ;
1644- for param in params {
1645- if let Ok ( snippet) =
1646- self . tcx . sess . source_map ( ) . span_to_snippet ( param. span )
1647- {
1648- if snippet. starts_with ( "&" ) && !snippet. starts_with ( "&'" ) {
1649- introduce_suggestion
1650- . push ( ( param. span , format ! ( "&'a {}" , & snippet[ 1 ..] ) ) ) ;
1651- } else if snippet. starts_with ( "&'_ " ) {
1652- introduce_suggestion
1653- . push ( ( param. span , format ! ( "&'a {}" , & snippet[ 4 ..] ) ) ) ;
1654- }
1655- }
1656- }
1657- introduce_suggestion. push ( ( span, sugg. to_string ( ) ) ) ;
1658- err. multipart_suggestion (
1659- & msg,
1660- introduce_suggestion,
1661- Applicability :: MaybeIncorrect ,
1662- ) ;
1663- if should_break {
1664- break ;
1665- }
1666- }
1667- } ;
1668-
1669- match (
1670- lifetime_names. len ( ) ,
1671- lifetime_names. iter ( ) . next ( ) ,
1672- snippet. as_ref ( ) . map ( |s| s. as_str ( ) ) ,
1673- ) {
1674- ( 1 , Some ( name) , Some ( "&" ) ) => {
1675- suggest_existing ( err, format ! ( "&{} " , name) ) ;
1676- }
1677- ( 1 , Some ( name) , Some ( "'_" ) ) => {
1678- suggest_existing ( err, name. to_string ( ) ) ;
1679- }
1680- ( 1 , Some ( name) , Some ( snippet) ) if !snippet. ends_with ( ">" ) => {
1681- suggest_existing ( err, format ! ( "{}<{}>" , snippet, name) ) ;
1682- }
1683- ( 0 , _, Some ( "&" ) ) => {
1684- suggest_new ( err, "&'a " ) ;
1685- }
1686- ( 0 , _, Some ( "'_" ) ) => {
1687- suggest_new ( err, "'a" ) ;
1688- }
1689- ( 0 , _, Some ( snippet) ) if !snippet. ends_with ( ">" ) => {
1690- suggest_new ( err, & format ! ( "{}<'a>" , snippet) ) ;
1691- }
1692- _ => {
1693- err. span_label ( span, "expected lifetime parameter" ) ;
1694- }
1695- }
1696- }
1697- }
1698- }
0 commit comments