@@ -43,12 +43,6 @@ pub trait InferCtxtExt<'tcx> {
43
43
body_id : hir:: HirId ,
44
44
) ;
45
45
46
- fn suggest_borrow_on_unsized_slice (
47
- & self ,
48
- code : & ObligationCauseCode < ' tcx > ,
49
- err : & mut DiagnosticBuilder < ' _ > ,
50
- ) ;
51
-
52
46
fn suggest_dereferences (
53
47
& self ,
54
48
obligation : & PredicateObligation < ' tcx > ,
@@ -515,32 +509,6 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
515
509
}
516
510
}
517
511
518
- /// When encountering an assignment of an unsized trait, like `let x = ""[..];`, provide a
519
- /// suggestion to borrow the initializer in order to use have a slice instead.
520
- fn suggest_borrow_on_unsized_slice (
521
- & self ,
522
- code : & ObligationCauseCode < ' tcx > ,
523
- err : & mut DiagnosticBuilder < ' _ > ,
524
- ) {
525
- if let & ObligationCauseCode :: VariableType ( hir_id) = code {
526
- let parent_node = self . tcx . hir ( ) . get_parent_node ( hir_id) ;
527
- if let Some ( Node :: Local ( ref local) ) = self . tcx . hir ( ) . find ( parent_node) {
528
- if let Some ( ref expr) = local. init {
529
- if let hir:: ExprKind :: Index ( _, _) = expr. kind {
530
- if let Ok ( snippet) = self . tcx . sess . source_map ( ) . span_to_snippet ( expr. span ) {
531
- err. span_suggestion (
532
- expr. span ,
533
- "consider borrowing here" ,
534
- format ! ( "&{}" , snippet) ,
535
- Applicability :: MachineApplicable ,
536
- ) ;
537
- }
538
- }
539
- }
540
- }
541
- }
542
- }
543
-
544
512
/// Given a closure's `DefId`, return the given name of the closure.
545
513
///
546
514
/// This doesn't account for reassignments, but it's only used for suggestions.
@@ -1817,8 +1785,37 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
1817
1785
}
1818
1786
}
1819
1787
}
1820
- ObligationCauseCode :: VariableType ( _) => {
1821
- err. note ( "all local variables must have a statically known size" ) ;
1788
+ ObligationCauseCode :: VariableType ( hir_id) => {
1789
+ let parent_node = self . tcx . hir ( ) . get_parent_node ( hir_id) ;
1790
+ match self . tcx . hir ( ) . find ( parent_node) {
1791
+ Some ( Node :: Local ( hir:: Local {
1792
+ init : Some ( hir:: Expr { kind : hir:: ExprKind :: Index ( _, _) , span, .. } ) ,
1793
+ ..
1794
+ } ) ) => {
1795
+ // When encountering an assignment of an unsized trait, like
1796
+ // `let x = ""[..];`, provide a suggestion to borrow the initializer in
1797
+ // order to use have a slice instead.
1798
+ err. span_suggestion_verbose (
1799
+ span. shrink_to_lo ( ) ,
1800
+ "consider borrowing here" ,
1801
+ "&" . to_owned ( ) ,
1802
+ Applicability :: MachineApplicable ,
1803
+ ) ;
1804
+ err. note ( "all local variables must have a statically known size" ) ;
1805
+ }
1806
+ Some ( Node :: Param ( param) ) => {
1807
+ err. span_suggestion_verbose (
1808
+ param. ty_span . shrink_to_lo ( ) ,
1809
+ "function arguments must have a statically known size, borrowed types \
1810
+ always have a known size",
1811
+ "&" . to_owned ( ) ,
1812
+ Applicability :: MachineApplicable ,
1813
+ ) ;
1814
+ }
1815
+ _ => {
1816
+ err. note ( "all local variables must have a statically known size" ) ;
1817
+ }
1818
+ }
1822
1819
if !self . tcx . features ( ) . unsized_locals {
1823
1820
err. help ( "unsized locals are gated as an unstable feature" ) ;
1824
1821
}
0 commit comments