提交 164f0105 编写于 作者: G Guillaume Gomez

Add safe_suggestion attribute

上级 601bbf26
......@@ -28,6 +28,7 @@
#![cfg_attr(test, allow(deprecated))] // rand
#![cfg_attr(not(stage0), deny(warnings))]
#![cfg_attr(not(stage0), feature(safe_suggestion))]
#![feature(alloc)]
#![feature(allow_internal_unstable)]
......
......@@ -1231,6 +1231,7 @@ pub unsafe fn as_mut_vec(&mut self) -> &mut Vec<u8> {
/// assert_eq!(a.len(), 3);
/// ```
#[inline]
#[cfg_attr(not(stage0), safe_suggestion)]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn len(&self) -> usize {
self.vec.len()
......
......@@ -587,19 +587,29 @@ pub fn note_type_err(&self,
// look for expected with found id
self.tcx.populate_inherent_implementations_for_type_if_necessary(found);
if let Some(impl_infos) = self.tcx.inherent_impls.borrow().get(&found) {
let mut methods = Vec::new();
let mut methods: Vec<(Option<ast::Attribute>, DefId, ImplOrTraitItem<'tcx>)> = Vec::new();
for impl_ in impl_infos {
methods.append(&mut self.tcx
.impl_or_trait_items(*impl_)
.iter()
.map(|&did| self.tcx.impl_or_trait_item(did))
.filter(|x| {
.map(|&did| (None, did, self.tcx.impl_or_trait_item(did)))
.filter(|&(_, _, ref x)| {
self.matches_return_type(x, &expected_ty)
})
.collect());
}
for method in methods {
println!("==> {:?}", method.name());
let safe_suggestions: Vec<_> = methods.iter()
.map(|&(_, ref id, ref x)| (self.find_attr(*id, "safe_suggestion"), id, x))
.filter(|&(ref res, _, _)| res.is_some())
.collect();
if safe_suggestions.len() > 0 {
for (_, _, method) in safe_suggestions {
println!("safe ==> {:?}", method.name());
}
} else {
for &(_, _, ref method) in methods.iter() {
println!("not safe ==> {:?}", method.name());
}
}
}
}
......@@ -615,6 +625,15 @@ pub fn note_type_err(&self,
self.tcx.note_and_explain_type_err(diag, terr, span);
}
fn find_attr(&self, def_id: DefId, attr_name: &str) -> Option<ast::Attribute> {
for item in self.tcx.get_attrs(def_id).iter() {
if item.check_name(attr_name) {
return Some(item.clone());
}
}
None
}
pub fn report_and_explain_type_error(&self,
trace: TypeTrace<'tcx>,
terr: &TypeError<'tcx>)
......
......@@ -304,6 +304,7 @@ pub fn new() -> Features {
// Allows using `Self` and associated types in struct expressions and patterns.
(active, more_struct_aliases, "1.14.0", Some(37544)),
// Allows #[link(..., cfg(..))]
(active, link_cfg, "1.14.0", Some(37406)),
......@@ -314,6 +315,9 @@ pub fn new() -> Features {
// Allows #[target_feature(...)]
(active, target_feature, "1.15.0", None),
// Allow safe suggestions for potential type conversions.
(active, safe_suggestion, "1.0.0", Some(37384)),
);
declare_features! (
......@@ -648,6 +652,11 @@ pub fn deprecated_attributes() -> Vec<&'static (&'static str, AttributeType, Att
"internal implementation detail",
cfg_fn!(rustc_attrs))),
("safe_suggestion", Whitelisted, Gated("safe_suggestion",
"the `#[safe_suggestion]` attribute \
is an experimental feature",
cfg_fn!(safe_suggestion))),
// FIXME: #14408 whitelist docs since rustdoc looks at them
("doc", Whitelisted, Ungated),
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册