1
Fork 0

Use BTreeMap to store attributes.

This commit is contained in:
Camille GILLOT 2021-01-24 17:14:17 +01:00
parent 90a562c7ff
commit 38d9d09a58
10 changed files with 117 additions and 47 deletions

View file

@ -16,6 +16,7 @@ use rustc_target::spec::abi::Abi;
use std::borrow::Cow;
use std::cell::Cell;
use std::collections::BTreeMap;
use std::vec;
pub fn id_to_string(map: &dyn rustc_hir::intravisit::Map<'_>, hir_id: hir::HirId) -> String {
@ -82,7 +83,7 @@ impl PpAnn for &dyn rustc_hir::intravisit::Map<'_> {
pub struct State<'a> {
pub s: pp::Printer,
comments: Option<Comments<'a>>,
attrs: &'a hir::HirIdVec<&'a [ast::Attribute]>,
attrs: &'a BTreeMap<hir::HirId, &'a [ast::Attribute]>,
ann: &'a (dyn PpAnn + 'a),
}
@ -169,7 +170,7 @@ pub fn print_crate<'a>(
// When printing the AST, we sometimes need to inject `#[no_std]` here.
// Since you can't compile the HIR, it's not necessary.
s.print_mod(&krate.item.module, krate.attrs[hir::CRATE_HIR_ID]);
s.print_mod(&krate.item.module, s.attrs(hir::CRATE_HIR_ID));
s.print_remaining_comments();
s.s.eof()
}
@ -179,7 +180,7 @@ impl<'a> State<'a> {
sm: &'a SourceMap,
filename: FileName,
input: String,
attrs: &'a hir::HirIdVec<&[ast::Attribute]>,
attrs: &'a BTreeMap<hir::HirId, &[ast::Attribute]>,
ann: &'a dyn PpAnn,
) -> State<'a> {
State {
@ -191,7 +192,7 @@ impl<'a> State<'a> {
}
fn attrs(&self, id: hir::HirId) -> &'a [ast::Attribute] {
self.attrs.get(id).map_or(&[], |la| *la)
self.attrs.get(&id).map_or(&[], |la| *la)
}
}
@ -200,7 +201,7 @@ where
F: FnOnce(&mut State<'_>),
{
let mut printer =
State { s: pp::mk_printer(), comments: None, attrs: &hir::HirIdVec::default(), ann };
State { s: pp::mk_printer(), comments: None, attrs: &BTreeMap::default(), ann };
f(&mut printer);
printer.s.eof()
}