Skip to content

Commit c829db3

Browse files
committed
review comments
1 parent 5e07be4 commit c829db3

File tree

3 files changed

+32
-29
lines changed

3 files changed

+32
-29
lines changed

src/librustc_hir/hir.rs

-26
Original file line numberDiff line numberDiff line change
@@ -437,32 +437,6 @@ impl GenericParam<'hir> {
437437
}
438438
}
439439

440-
pub trait NextTypeParamName {
441-
fn next_type_param_name(&self, name: Option<&str>) -> String;
442-
}
443-
444-
impl NextTypeParamName for &[GenericParam<'_>] {
445-
fn next_type_param_name(&self, name: Option<&str>) -> String {
446-
// This is the whitelist of possible parameter names that we might suggest.
447-
let name = name.and_then(|n| n.chars().next()).map(|c| c.to_string().to_uppercase());
448-
let name = name.as_ref().map(|s| s.as_str());
449-
let possible_names = [name.unwrap_or("T"), "T", "U", "V", "X", "Y", "Z", "A", "B", "C"];
450-
let used_names = self
451-
.iter()
452-
.filter_map(|p| match p.name {
453-
ParamName::Plain(ident) => Some(ident.name),
454-
_ => None,
455-
})
456-
.collect::<Vec<_>>();
457-
458-
possible_names
459-
.iter()
460-
.find(|n| !used_names.contains(&Symbol::intern(n)))
461-
.unwrap_or(&"ParamName")
462-
.to_string()
463-
}
464-
}
465-
466440
#[derive(Default)]
467441
pub struct GenericParamCount {
468442
pub lifetimes: usize,

src/librustc_trait_selection/traits/error_reporting/suggestions.rs

+30-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ use rustc_hir as hir;
1010
use rustc_hir::def::DefKind;
1111
use rustc_hir::def_id::DefId;
1212
use rustc_hir::intravisit::Visitor;
13-
use rustc_hir::{NextTypeParamName, Node};
13+
use rustc_hir::Node;
1414
use rustc_middle::ty::TypeckTables;
1515
use rustc_middle::ty::{
1616
self, AdtKind, DefIdTree, ToPredicate, Ty, TyCtxt, TypeFoldable, WithConstness,
1717
};
18-
use rustc_span::symbol::{kw, sym};
18+
use rustc_span::symbol::{kw, sym, Symbol};
1919
use rustc_span::{MultiSpan, Span, DUMMY_SP};
2020
use std::fmt;
2121

@@ -248,6 +248,8 @@ fn suggest_restriction(
248248
sugg.extend(ty_spans.into_iter().map(|s| (s, type_param_name.to_string())));
249249

250250
// Suggest `fn foo<T: Trait>(t: T) where <T as Trait>::A: Bound`.
251+
// FIXME: once `#![feature(associated_type_bounds)]` is stabilized, we should suggest
252+
// `fn foo(t: impl Trait<A: Bound>)` instead.
251253
err.multipart_suggestion(
252254
"introduce a type parameter with a trait bound instead of using `impl Trait`",
253255
sugg,
@@ -1694,3 +1696,29 @@ impl<'v> Visitor<'v> for ReturnsVisitor<'v> {
16941696
hir::intravisit::walk_body(self, body);
16951697
}
16961698
}
1699+
1700+
pub trait NextTypeParamName {
1701+
fn next_type_param_name(&self, name: Option<&str>) -> String;
1702+
}
1703+
1704+
impl NextTypeParamName for &[hir::GenericParam<'_>] {
1705+
fn next_type_param_name(&self, name: Option<&str>) -> String {
1706+
// This is the whitelist of possible parameter names that we might suggest.
1707+
let name = name.and_then(|n| n.chars().next()).map(|c| c.to_string().to_uppercase());
1708+
let name = name.as_ref().map(|s| s.as_str());
1709+
let possible_names = [name.unwrap_or("T"), "T", "U", "V", "X", "Y", "Z", "A", "B", "C"];
1710+
let used_names = self
1711+
.iter()
1712+
.filter_map(|p| match p.name {
1713+
hir::ParamName::Plain(ident) => Some(ident.name),
1714+
_ => None,
1715+
})
1716+
.collect::<Vec<_>>();
1717+
1718+
possible_names
1719+
.iter()
1720+
.find(|n| !used_names.contains(&Symbol::intern(n)))
1721+
.unwrap_or(&"ParamName")
1722+
.to_string()
1723+
}
1724+
}

src/librustc_typeck/collect.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use rustc_hir::def::{CtorKind, DefKind, Res};
2929
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
3030
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
3131
use rustc_hir::weak_lang_items;
32-
use rustc_hir::{GenericParamKind, NextTypeParamName, Node, Unsafety};
32+
use rustc_hir::{GenericParamKind, Node, Unsafety};
3333
use rustc_middle::hir::map::blocks::FnLikeNode;
3434
use rustc_middle::hir::map::Map;
3535
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
@@ -45,6 +45,7 @@ use rustc_session::parse::feature_err;
4545
use rustc_span::symbol::{kw, sym, Symbol};
4646
use rustc_span::{Span, DUMMY_SP};
4747
use rustc_target::spec::abi;
48+
use rustc_trait_selection::traits::error_reporting::suggestions::NextTypeParamName;
4849

4950
mod type_of;
5051

0 commit comments

Comments
 (0)