Skip to content

Commit

Permalink
Move get_lib_features query in librustc_passes.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Dec 29, 2019
1 parent ec3a9f6 commit 5768162
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 33 deletions.
25 changes: 24 additions & 1 deletion src/librustc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,30 @@ pub mod middle {
pub mod exported_symbols;
pub mod free_region;
pub mod lang_items;
pub mod lib_features;
pub mod lib_features {
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use syntax::symbol::Symbol;

#[derive(HashStable)]
pub struct LibFeatures {
// A map from feature to stabilisation version.
pub stable: FxHashMap<Symbol, Symbol>,
pub unstable: FxHashSet<Symbol>,
}

impl LibFeatures {
pub fn to_vec(&self) -> Vec<(Symbol, Option<Symbol>)> {
let mut all_features: Vec<_> = self
.stable
.iter()
.map(|(f, s)| (*f, Some(*s)))
.chain(self.unstable.iter().map(|f| (*f, None)))
.collect();
all_features.sort_unstable_by_key(|f| f.0.as_str());
all_features
}
}
}
pub mod privacy;
pub mod recursion_limit;
pub mod region;
Expand Down
4 changes: 0 additions & 4 deletions src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2751,10 +2751,6 @@ pub fn provide(providers: &mut ty::query::Providers<'_>) {
assert_eq!(id, LOCAL_CRATE);
tcx.crate_name
};
providers.get_lib_features = |tcx, id| {
assert_eq!(id, LOCAL_CRATE);
tcx.arena.alloc(middle::lib_features::collect(tcx))
};
providers.get_lang_items = |tcx, id| {
assert_eq!(id, LOCAL_CRATE);
tcx.arena.alloc(middle::lang_items::collect(tcx))
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_passes/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub mod entry;
pub mod hir_stats;
mod intrinsicck;
pub mod layout_test;
mod lib_features;
mod liveness;
pub mod loops;
mod reachable;
Expand All @@ -35,6 +36,7 @@ pub fn provide(providers: &mut Providers<'_>) {
check_const::provide(providers);
diagnostic_items::provide(providers);
entry::provide(providers);
lib_features::provide(providers);
loops::provide(providers);
liveness::provide(providers);
intrinsicck::provide(providers);
Expand Down
44 changes: 16 additions & 28 deletions src/librustc_passes/lib_features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,19 @@
// and `#[unstable (..)]`), but are not declared in one single location
// (unlike lang features), which means we need to collect them instead.

use crate::hir::intravisit::{self, NestedVisitorMap, Visitor};
use crate::ty::TyCtxt;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_macros::HashStable;
use rustc::hir::def_id::LOCAL_CRATE;
use rustc::hir::intravisit::{self, NestedVisitorMap, Visitor};
use rustc::middle::lib_features::LibFeatures;
use rustc::ty::query::Providers;
use rustc::ty::TyCtxt;
use syntax::ast::{Attribute, MetaItem, MetaItemKind};
use syntax::symbol::Symbol;
use syntax_pos::{sym, Span};

use rustc_error_codes::*;

#[derive(HashStable)]
pub struct LibFeatures {
// A map from feature to stabilisation version.
pub stable: FxHashMap<Symbol, Symbol>,
pub unstable: FxHashSet<Symbol>,
}

impl LibFeatures {
fn new() -> LibFeatures {
LibFeatures { stable: Default::default(), unstable: Default::default() }
}

pub fn to_vec(&self) -> Vec<(Symbol, Option<Symbol>)> {
let mut all_features: Vec<_> = self
.stable
.iter()
.map(|(f, s)| (*f, Some(*s)))
.chain(self.unstable.iter().map(|f| (*f, None)))
.collect();
all_features.sort_unstable_by_key(|f| f.0.as_str());
all_features
}
fn new_lib_features() -> LibFeatures {
LibFeatures { stable: Default::default(), unstable: Default::default() }
}

pub struct LibFeatureCollector<'tcx> {
Expand All @@ -45,7 +26,7 @@ pub struct LibFeatureCollector<'tcx> {

impl LibFeatureCollector<'tcx> {
fn new(tcx: TyCtxt<'tcx>) -> LibFeatureCollector<'tcx> {
LibFeatureCollector { tcx, lib_features: LibFeatures::new() }
LibFeatureCollector { tcx, lib_features: new_lib_features() }
}

fn extract(&self, attr: &Attribute) -> Option<(Symbol, Option<Symbol>, Span)> {
Expand Down Expand Up @@ -142,7 +123,7 @@ impl Visitor<'tcx> for LibFeatureCollector<'tcx> {
}
}

pub fn collect(tcx: TyCtxt<'_>) -> LibFeatures {
fn collect(tcx: TyCtxt<'_>) -> LibFeatures {
let mut collector = LibFeatureCollector::new(tcx);
let krate = tcx.hir().krate();
for attr in krate.non_exported_macro_attrs {
Expand All @@ -151,3 +132,10 @@ pub fn collect(tcx: TyCtxt<'_>) -> LibFeatures {
intravisit::walk_crate(&mut collector, krate);
collector.lib_features
}

pub fn provide(providers: &mut Providers<'_>) {
providers.get_lib_features = |tcx, id| {
assert_eq!(id, LOCAL_CRATE);
tcx.arena.alloc(collect(tcx))
};
}

0 comments on commit 5768162

Please sign in to comment.