2019-12-31 21:25:16 +01:00
|
|
|
use errors::struct_span_err;
|
2017-04-25 11:45:59 +02:00
|
|
|
use rustc::ty::TyCtxt;
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_hir::itemlikevisit::ItemLikeVisitor;
|
2020-01-01 19:30:57 +01:00
|
|
|
use rustc_span::symbol::sym;
|
2017-04-25 11:45:59 +02:00
|
|
|
|
2019-11-11 22:46:56 +01:00
|
|
|
use rustc_error_codes::*;
|
|
|
|
|
2019-06-21 23:49:03 +02:00
|
|
|
pub fn test_variance(tcx: TyCtxt<'_>) {
|
2018-12-04 13:45:36 +01:00
|
|
|
tcx.hir().krate().visit_all_item_likes(&mut VarianceTest { tcx });
|
2017-04-25 11:45:59 +02:00
|
|
|
}
|
|
|
|
|
2019-06-11 21:03:44 +02:00
|
|
|
struct VarianceTest<'tcx> {
|
2019-06-13 23:48:52 +02:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2017-04-25 11:45:59 +02:00
|
|
|
}
|
|
|
|
|
2019-06-11 21:03:44 +02:00
|
|
|
impl ItemLikeVisitor<'tcx> for VarianceTest<'tcx> {
|
2019-11-28 19:28:50 +01:00
|
|
|
fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
|
2019-06-27 11:28:14 +02:00
|
|
|
let item_def_id = self.tcx.hir().local_def_id(item.hir_id);
|
2017-04-25 11:45:59 +02:00
|
|
|
|
|
|
|
// For unit testing: check for a special "rustc_variance"
|
|
|
|
// attribute and report an error with various results if found.
|
2019-05-08 05:21:18 +02:00
|
|
|
if self.tcx.has_attr(item_def_id, sym::rustc_variance) {
|
2017-05-05 20:34:42 +02:00
|
|
|
let variances_of = self.tcx.variances_of(item_def_id);
|
2019-12-31 21:25:16 +01:00
|
|
|
struct_span_err!(self.tcx.sess, item.span, E0208, "{:?}", variances_of).emit();
|
2017-04-25 11:45:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 23:42:04 +01:00
|
|
|
fn visit_trait_item(&mut self, _: &'tcx hir::TraitItem<'tcx>) {}
|
|
|
|
fn visit_impl_item(&mut self, _: &'tcx hir::ImplItem<'tcx>) {}
|
2017-04-25 11:45:59 +02:00
|
|
|
}
|