Add safe_suggestion attribute · rust-lang/rust@164f010 (original) (raw)

`@@ -587,19 +587,29 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {

`

587

587

`// look for expected with found id

`

588

588

`self.tcx.populate_inherent_implementations_for_type_if_necessary(found);

`

589

589

`if let Some(impl_infos) = self.tcx.inherent_impls.borrow().get(&found) {

`

590

``

`-

let mut methods = Vec::new();

`

``

590

`+

let mut methods: Vec<(Optionast::Attribute, DefId, ImplOrTraitItem<'tcx>)> = Vec::new();

`

591

591

`for impl_ in impl_infos {

`

592

592

` methods.append(&mut self.tcx

`

593

593

`.impl_or_trait_items(*impl_)

`

594

594

`.iter()

`

595

``

`-

.map(|&did| self.tcx.impl_or_trait_item(did))

`

596

``

`-

.filter(|x| {

`

``

595

`+

.map(|&did| (None, did, self.tcx.impl_or_trait_item(did)))

`

``

596

`+

.filter(|&(_, _, ref x)| {

`

597

597

`self.matches_return_type(x, &expected_ty)

`

598

598

`})

`

599

599

`.collect());

`

600

600

`}

`

601

``

`-

for method in methods {

`

602

``

`-

println!("==> {:?}", method.name());

`

``

601

`+

let safe_suggestions: Vec<_> = methods.iter()

`

``

602

`+

.map(|&(_, ref id, ref x)| (self.find_attr(*id, "safe_suggestion"), id, x))

`

``

603

`+

.filter(|&(ref res, _, _)| res.is_some())

`

``

604

`+

.collect();

`

``

605

`+

if safe_suggestions.len() > 0 {

`

``

606

`+

for (_, _, method) in safe_suggestions {

`

``

607

`+

println!("safe ==> {:?}", method.name());

`

``

608

`+

}

`

``

609

`+

} else {

`

``

610

`+

for &(_, _, ref method) in methods.iter() {

`

``

611

`+

println!("not safe ==> {:?}", method.name());

`

``

612

`+

}

`

603

613

`}

`

604

614

`}

`

605

615

`}

`

`@@ -615,6 +625,15 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {

`

615

625

`self.tcx.note_and_explain_type_err(diag, terr, span);

`

616

626

`}

`

617

627

``

``

628

`+

fn find_attr(&self, def_id: DefId, attr_name: &str) -> Optionast::Attribute {

`

``

629

`+

for item in self.tcx.get_attrs(def_id).iter() {

`

``

630

`+

if item.check_name(attr_name) {

`

``

631

`+

return Some(item.clone());

`

``

632

`+

}

`

``

633

`+

}

`

``

634

`+

None

`

``

635

`+

}

`

``

636

+

618

637

`pub fn report_and_explain_type_error(&self,

`

619

638

`trace: TypeTrace<'tcx>,

`

620

639

`terr: &TypeError<'tcx>)

`