Review comments.
This commit is contained in:
parent
513eb744c0
commit
fc73e196d9
@ -31,7 +31,7 @@ impl<'tcx> TyCtxt<'tcx> {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn fn_trait_lang_item(&self, id: DefId) -> Option<ty::ClosureKind> {
|
||||
pub fn fn_trait_kind_from_lang_item(&self, id: DefId) -> Option<ty::ClosureKind> {
|
||||
let items = self.lang_items();
|
||||
match Some(id) {
|
||||
x if x == items.fn_trait() => Some(ty::ClosureKind::Fn),
|
||||
|
@ -1634,7 +1634,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
|
||||
obligation: &TraitObligation<'tcx>,
|
||||
candidates: &mut SelectionCandidateSet<'tcx>,
|
||||
) -> Result<(), SelectionError<'tcx>> {
|
||||
let kind = match self.tcx().fn_trait_lang_item(obligation.predicate.def_id()) {
|
||||
let kind = match self.tcx().fn_trait_kind_from_lang_item(obligation.predicate.def_id()) {
|
||||
Some(k) => k,
|
||||
None => {
|
||||
return Ok(());
|
||||
@ -1677,7 +1677,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
|
||||
candidates: &mut SelectionCandidateSet<'tcx>,
|
||||
) -> Result<(), SelectionError<'tcx>> {
|
||||
// We provide impl of all fn traits for fn pointers.
|
||||
if self.tcx().fn_trait_lang_item(obligation.predicate.def_id()).is_none() {
|
||||
if self.tcx().fn_trait_kind_from_lang_item(obligation.predicate.def_id()).is_none() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
@ -2889,7 +2889,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
|
||||
|
||||
let kind = self
|
||||
.tcx()
|
||||
.fn_trait_lang_item(obligation.predicate.def_id())
|
||||
.fn_trait_kind_from_lang_item(obligation.predicate.def_id())
|
||||
.unwrap_or_else(|| bug!("closure candidate for non-fn trait {:?}", obligation));
|
||||
|
||||
// Okay to skip binder because the substs on closure types never
|
||||
|
@ -450,7 +450,7 @@ fn resolve_associated_item<'tcx>(
|
||||
substs: generator_data.substs,
|
||||
}),
|
||||
traits::VtableClosure(closure_data) => {
|
||||
let trait_closure_kind = tcx.fn_trait_lang_item(trait_id).unwrap();
|
||||
let trait_closure_kind = tcx.fn_trait_kind_from_lang_item(trait_id).unwrap();
|
||||
Some(Instance::resolve_closure(
|
||||
tcx,
|
||||
closure_data.closure_def_id,
|
||||
|
@ -724,7 +724,7 @@ pub trait PrettyPrinter<'tcx>:
|
||||
let mut resugared = false;
|
||||
|
||||
// Special-case `Fn(...) -> ...` and resugar it.
|
||||
let fn_trait_kind = self.tcx().fn_trait_lang_item(principal.def_id);
|
||||
let fn_trait_kind = self.tcx().fn_trait_kind_from_lang_item(principal.def_id);
|
||||
if !self.tcx().sess.verbose() && fn_trait_kind.is_some() {
|
||||
if let ty::Tuple(ref args) = principal.substs.type_at(0).kind {
|
||||
let mut projections = predicates.projection_bounds();
|
||||
|
@ -28,76 +28,76 @@ macro_rules! language_item_table {
|
||||
$( $variant:ident, $name:expr, $method:ident, $target:path; )*
|
||||
) => {
|
||||
|
||||
enum_from_u32! {
|
||||
/// A representation of all the valid language items in Rust.
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
|
||||
pub enum LangItem {
|
||||
$($variant,)*
|
||||
}
|
||||
}
|
||||
|
||||
impl LangItem {
|
||||
/// Returns the `name` in `#[lang = "$name"]`.
|
||||
/// For example, `LangItem::EqTraitLangItem`,
|
||||
/// that is `#[lang = "eq"]` would result in `"eq"`.
|
||||
pub fn name(self) -> &'static str {
|
||||
match self {
|
||||
$( $variant => $name, )*
|
||||
enum_from_u32! {
|
||||
/// A representation of all the valid language items in Rust.
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
|
||||
pub enum LangItem {
|
||||
$($variant,)*
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(HashStable_Generic)]
|
||||
pub struct LanguageItems {
|
||||
/// Mappings from lang items to their possibly found `DefId`s.
|
||||
/// The index corresponds to the order in `LangItem`.
|
||||
pub items: Vec<Option<DefId>>,
|
||||
/// Lang items that were not found during collection.
|
||||
pub missing: Vec<LangItem>,
|
||||
}
|
||||
|
||||
impl LanguageItems {
|
||||
/// Construct an empty collection of lang items and no missing ones.
|
||||
pub fn new() -> Self {
|
||||
fn init_none(_: LangItem) -> Option<DefId> { None }
|
||||
|
||||
Self {
|
||||
items: vec![$(init_none($variant)),*],
|
||||
missing: Vec::new(),
|
||||
impl LangItem {
|
||||
/// Returns the `name` in `#[lang = "$name"]`.
|
||||
/// For example, `LangItem::EqTraitLangItem`,
|
||||
/// that is `#[lang = "eq"]` would result in `"eq"`.
|
||||
pub fn name(self) -> &'static str {
|
||||
match self {
|
||||
$( $variant => $name, )*
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the mappings to the possibly found `DefId`s for each lang item.
|
||||
pub fn items(&self) -> &[Option<DefId>] {
|
||||
&*self.items
|
||||
}
|
||||
|
||||
/// Requires that a given `LangItem` was bound and returns the corresponding `DefId`.
|
||||
/// If it wasn't bound, e.g. due to a missing `#[lang = "<it.name()>"]`,
|
||||
/// returns an error message as a string.
|
||||
pub fn require(&self, it: LangItem) -> Result<DefId, String> {
|
||||
self.items[it as usize].ok_or_else(|| format!("requires `{}` lang_item", it.name()))
|
||||
}
|
||||
|
||||
$(
|
||||
/// Returns the corresponding `DefId` for the lang item
|
||||
#[doc = $name]
|
||||
/// if it exists.
|
||||
#[allow(dead_code)]
|
||||
pub fn $method(&self) -> Option<DefId> {
|
||||
self.items[$variant as usize]
|
||||
#[derive(HashStable_Generic)]
|
||||
pub struct LanguageItems {
|
||||
/// Mappings from lang items to their possibly found `DefId`s.
|
||||
/// The index corresponds to the order in `LangItem`.
|
||||
pub items: Vec<Option<DefId>>,
|
||||
/// Lang items that were not found during collection.
|
||||
pub missing: Vec<LangItem>,
|
||||
}
|
||||
)*
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
/// A mapping from the name of the lang item to its order and the form it must be of.
|
||||
pub static ref ITEM_REFS: FxHashMap<&'static str, (usize, Target)> = {
|
||||
let mut item_refs = FxHashMap::default();
|
||||
$( item_refs.insert($name, ($variant as usize, $target)); )*
|
||||
item_refs
|
||||
};
|
||||
}
|
||||
impl LanguageItems {
|
||||
/// Construct an empty collection of lang items and no missing ones.
|
||||
pub fn new() -> Self {
|
||||
fn init_none(_: LangItem) -> Option<DefId> { None }
|
||||
|
||||
Self {
|
||||
items: vec![$(init_none($variant)),*],
|
||||
missing: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the mappings to the possibly found `DefId`s for each lang item.
|
||||
pub fn items(&self) -> &[Option<DefId>] {
|
||||
&*self.items
|
||||
}
|
||||
|
||||
/// Requires that a given `LangItem` was bound and returns the corresponding `DefId`.
|
||||
/// If it wasn't bound, e.g. due to a missing `#[lang = "<it.name()>"]`,
|
||||
/// returns an error message as a string.
|
||||
pub fn require(&self, it: LangItem) -> Result<DefId, String> {
|
||||
self.items[it as usize].ok_or_else(|| format!("requires `{}` lang_item", it.name()))
|
||||
}
|
||||
|
||||
$(
|
||||
/// Returns the corresponding `DefId` for the lang item
|
||||
#[doc = $name]
|
||||
/// if it exists.
|
||||
#[allow(dead_code)]
|
||||
pub fn $method(&self) -> Option<DefId> {
|
||||
self.items[$variant as usize]
|
||||
}
|
||||
)*
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
/// A mapping from the name of the lang item to its order and the form it must be of.
|
||||
pub static ref ITEM_REFS: FxHashMap<&'static str, (usize, Target)> = {
|
||||
let mut item_refs = FxHashMap::default();
|
||||
$( item_refs.insert($name, ($variant as usize, $target)); )*
|
||||
item_refs
|
||||
};
|
||||
}
|
||||
|
||||
// End of the macro
|
||||
}
|
||||
|
@ -40,7 +40,7 @@ fn make_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: ty::InstanceDef<'tcx>) -> &'tcx
|
||||
),
|
||||
ty::InstanceDef::FnPtrShim(def_id, ty) => {
|
||||
let trait_ = tcx.trait_of_item(def_id).unwrap();
|
||||
let adjustment = match tcx.fn_trait_lang_item(trait_) {
|
||||
let adjustment = match tcx.fn_trait_kind_from_lang_item(trait_) {
|
||||
Some(ty::ClosureKind::FnOnce) => Adjustment::Identity,
|
||||
Some(ty::ClosureKind::FnMut) | Some(ty::ClosureKind::Fn) => Adjustment::Deref,
|
||||
None => bug!("fn pointer {:?} is not an fn", ty),
|
||||
|
@ -174,8 +174,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||
self.deduce_sig_from_projection(None, &pb)
|
||||
})
|
||||
.next();
|
||||
let kind =
|
||||
object_type.principal_def_id().and_then(|did| self.tcx.fn_trait_lang_item(did));
|
||||
let kind = object_type
|
||||
.principal_def_id()
|
||||
.and_then(|did| self.tcx.fn_trait_kind_from_lang_item(did));
|
||||
(sig, kind)
|
||||
}
|
||||
ty::Infer(ty::TyVar(vid)) => self.deduce_expectations_from_obligations(vid),
|
||||
@ -213,7 +214,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||
// many viable options, so pick the most restrictive.
|
||||
let expected_kind = self
|
||||
.obligations_for_self_ty(expected_vid)
|
||||
.filter_map(|(tr, _)| self.tcx.fn_trait_lang_item(tr.def_id()))
|
||||
.filter_map(|(tr, _)| self.tcx.fn_trait_kind_from_lang_item(tr.def_id()))
|
||||
.fold(None, |best, cur| Some(best.map_or(cur, |best| cmp::min(best, cur))));
|
||||
|
||||
(expected_sig, expected_kind)
|
||||
@ -236,7 +237,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||
|
||||
let trait_ref = projection.to_poly_trait_ref(tcx);
|
||||
|
||||
let is_fn = tcx.fn_trait_lang_item(trait_ref.def_id()).is_some();
|
||||
let is_fn = tcx.fn_trait_kind_from_lang_item(trait_ref.def_id()).is_some();
|
||||
let gen_trait = tcx.require_lang_item(lang_items::GeneratorTraitLangItem, cause_span);
|
||||
let is_gen = gen_trait == trait_ref.def_id();
|
||||
if !is_fn && !is_gen {
|
||||
|
@ -138,7 +138,7 @@ pub fn external_generic_args(
|
||||
|
||||
match trait_did {
|
||||
// Attempt to sugar an external path like Fn<(A, B,), C> to Fn(A, B) -> C
|
||||
Some(did) if cx.tcx.fn_trait_lang_item(did).is_some() => {
|
||||
Some(did) if cx.tcx.fn_trait_kind_from_lang_item(did).is_some() => {
|
||||
assert!(ty_kind.is_some());
|
||||
let inputs = match ty_kind {
|
||||
Some(ty::Tuple(ref tys)) => tys.iter().map(|t| t.expect_ty().clean(cx)).collect(),
|
||||
|
Loading…
x
Reference in New Issue
Block a user