Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ debug = 0 # Set this to 1 or 2 to get more useful backtraces in debugger.
# chalk-recursive = { path = "../chalk/chalk-recursive" }

# ungrammar = { path = "../ungrammar" }

# salsa = { path = "../salsa" }
2 changes: 1 addition & 1 deletion crates/hir/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ fn resolve_doc_path(
AttrDefId::MacroDefId(_) => return None,
};
let path = ast::Path::parse(link).ok()?;
let modpath = ModPath::from_src(path, &Hygiene::new_unhygienic()).unwrap();
let modpath = ModPath::from_src(db.upcast(), path, &Hygiene::new_unhygienic()).unwrap();
let resolved = resolver.resolve_module_path_in_items(db.upcast(), &modpath);
if resolved == PerNs::none() {
if let Some(trait_id) = resolver.resolve_module_path_in_trait_items(db.upcast(), &modpath) {
Expand Down
2 changes: 1 addition & 1 deletion crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1666,7 +1666,7 @@ impl Impl {
.value
.attrs()
.filter_map(|it| {
let path = ModPath::from_src(it.path()?, &hygenic)?;
let path = ModPath::from_src(db.upcast(), it.path()?, &hygenic)?;
if path.as_ident()?.to_string() == "derive" {
Some(it)
} else {
Expand Down
2 changes: 1 addition & 1 deletion crates/hir/src/source_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ impl SourceAnalyzer {

// This must be a normal source file rather than macro file.
let hygiene = Hygiene::new(db.upcast(), self.file_id);
let ctx = body::LowerCtx::with_hygiene(&hygiene);
let ctx = body::LowerCtx::with_hygiene(db.upcast(), &hygiene);
let hir_path = Path::from_src(path.clone(), &ctx)?;

// Case where path is a qualifier of another path, e.g. foo::bar::Baz where we
Expand Down
21 changes: 15 additions & 6 deletions crates/hir_def/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,17 @@ impl ops::Deref for AttrsWithOwner {
impl RawAttrs {
pub(crate) const EMPTY: Self = Self { entries: None };

pub(crate) fn new(owner: &dyn ast::AttrsOwner, hygiene: &Hygiene) -> Self {
pub(crate) fn new(
db: &dyn DefDatabase,
owner: &dyn ast::AttrsOwner,
hygiene: &Hygiene,
) -> Self {
let entries = collect_attrs(owner)
.enumerate()
.flat_map(|(i, attr)| {
let index = AttrId(i as u32);
match attr {
Either::Left(attr) => Attr::from_src(attr, hygiene, index),
Either::Left(attr) => Attr::from_src(db, attr, hygiene, index),
Either::Right(comment) => comment.doc_comment().map(|doc| Attr {
id: index,
input: Some(AttrInput::Literal(SmolStr::new(doc))),
Expand All @@ -116,7 +120,7 @@ impl RawAttrs {

fn from_attrs_owner(db: &dyn DefDatabase, owner: InFile<&dyn ast::AttrsOwner>) -> Self {
let hygiene = Hygiene::new(db.upcast(), owner.file_id);
Self::new(owner.value, &hygiene)
Self::new(db, owner.value, &hygiene)
}

pub(crate) fn merge(&self, other: Self) -> Self {
Expand Down Expand Up @@ -170,7 +174,7 @@ impl RawAttrs {
let attr = ast::Attr::parse(&format!("#[{}]", tree)).ok()?;
// FIXME hygiene
let hygiene = Hygiene::new_unhygienic();
Attr::from_src(attr, &hygiene, index)
Attr::from_src(db, attr, &hygiene, index)
});

let cfg_options = &crate_graph[krate].cfg_options;
Expand Down Expand Up @@ -627,8 +631,13 @@ pub enum AttrInput {
}

impl Attr {
fn from_src(ast: ast::Attr, hygiene: &Hygiene, id: AttrId) -> Option<Attr> {
let path = Interned::new(ModPath::from_src(ast.path()?, hygiene)?);
fn from_src(
db: &dyn DefDatabase,
ast: ast::Attr,
hygiene: &Hygiene,
id: AttrId,
) -> Option<Attr> {
let path = Interned::new(ModPath::from_src(db, ast.path()?, hygiene)?);
let input = if let Some(ast::Expr::Literal(lit)) = ast.expr() {
let value = match lit.kind() {
ast::LiteralKind::String(string) => string.value()?.into(),
Expand Down
6 changes: 3 additions & 3 deletions crates/hir_def/src/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl CfgExpander {
}

pub(crate) fn parse_attrs(&self, db: &dyn DefDatabase, owner: &dyn ast::AttrsOwner) -> Attrs {
RawAttrs::new(owner, &self.hygiene).filter(db, self.krate)
RawAttrs::new(db, owner, &self.hygiene).filter(db, self.krate)
}

pub(crate) fn is_cfg_enabled(&self, db: &dyn DefDatabase, owner: &dyn ast::AttrsOwner) -> bool {
Expand Down Expand Up @@ -192,8 +192,8 @@ impl Expander {
self.current_file_id
}

fn parse_path(&mut self, path: ast::Path) -> Option<Path> {
let ctx = LowerCtx::with_hygiene(&self.cfg_expander.hygiene);
fn parse_path(&mut self, db: &dyn DefDatabase, path: ast::Path) -> Option<Path> {
let ctx = LowerCtx::with_hygiene(db, &self.cfg_expander.hygiene);
Path::from_src(path, &ctx)
}

Expand Down
28 changes: 17 additions & 11 deletions crates/hir_def/src/body/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,25 @@ use crate::{

use super::{diagnostics::BodyDiagnostic, ExprSource, PatSource};

pub struct LowerCtx {
pub struct LowerCtx<'a> {
pub db: &'a dyn DefDatabase,
hygiene: Hygiene,
file_id: Option<HirFileId>,
source_ast_id_map: Option<Arc<AstIdMap>>,
}

impl LowerCtx {
pub fn new(db: &dyn DefDatabase, file_id: HirFileId) -> Self {
impl<'a> LowerCtx<'a> {
pub fn new(db: &'a dyn DefDatabase, file_id: HirFileId) -> Self {
LowerCtx {
db,
hygiene: Hygiene::new(db.upcast(), file_id),
file_id: Some(file_id),
source_ast_id_map: Some(db.ast_id_map(file_id)),
}
}

pub fn with_hygiene(hygiene: &Hygiene) -> Self {
LowerCtx { hygiene: hygiene.clone(), file_id: None, source_ast_id_map: None }
pub fn with_hygiene(db: &'a dyn DefDatabase, hygiene: &Hygiene) -> Self {
LowerCtx { db, hygiene: hygiene.clone(), file_id: None, source_ast_id_map: None }
}

pub(crate) fn hygiene(&self) -> &Hygiene {
Expand Down Expand Up @@ -145,7 +147,7 @@ impl ExprCollector<'_> {
(self.body, self.source_map)
}

fn ctx(&self) -> LowerCtx {
fn ctx(&self) -> LowerCtx<'_> {
LowerCtx::new(self.db, self.expander.current_file_id)
}

Expand Down Expand Up @@ -376,7 +378,7 @@ impl ExprCollector<'_> {
ast::Expr::PathExpr(e) => {
let path = e
.path()
.and_then(|path| self.expander.parse_path(path))
.and_then(|path| self.expander.parse_path(self.db, path))
.map(Expr::Path)
.unwrap_or(Expr::Missing);
self.alloc_expr(path, syntax_ptr)
Expand Down Expand Up @@ -408,7 +410,8 @@ impl ExprCollector<'_> {
self.alloc_expr(Expr::Yield { expr }, syntax_ptr)
}
ast::Expr::RecordExpr(e) => {
let path = e.path().and_then(|path| self.expander.parse_path(path)).map(Box::new);
let path =
e.path().and_then(|path| self.expander.parse_path(self.db, path)).map(Box::new);
let record_lit = if let Some(nfl) = e.record_expr_field_list() {
let fields = nfl
.fields()
Expand Down Expand Up @@ -791,7 +794,8 @@ impl ExprCollector<'_> {
}
}
ast::Pat::TupleStructPat(p) => {
let path = p.path().and_then(|path| self.expander.parse_path(path)).map(Box::new);
let path =
p.path().and_then(|path| self.expander.parse_path(self.db, path)).map(Box::new);
let (args, ellipsis) = self.collect_tuple_pat(p.fields());
Pat::TupleStruct { path, args, ellipsis }
}
Expand All @@ -801,7 +805,8 @@ impl ExprCollector<'_> {
Pat::Ref { pat, mutability }
}
ast::Pat::PathPat(p) => {
let path = p.path().and_then(|path| self.expander.parse_path(path)).map(Box::new);
let path =
p.path().and_then(|path| self.expander.parse_path(self.db, path)).map(Box::new);
path.map(Pat::Path).unwrap_or(Pat::Missing)
}
ast::Pat::OrPat(p) => {
Expand All @@ -815,7 +820,8 @@ impl ExprCollector<'_> {
}
ast::Pat::WildcardPat(_) => Pat::Wild,
ast::Pat::RecordPat(p) => {
let path = p.path().and_then(|path| self.expander.parse_path(path)).map(Box::new);
let path =
p.path().and_then(|path| self.expander.parse_path(self.db, path)).map(Box::new);
let args: Vec<_> = p
.record_pat_field_list()
.expect("every struct should have a field list")
Expand Down
2 changes: 1 addition & 1 deletion crates/hir_def/src/find_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ mod tests {
let parsed_path_file = syntax::SourceFile::parse(&format!("use {};", path));
let ast_path =
parsed_path_file.syntax_node().descendants().find_map(syntax::ast::Path::cast).unwrap();
let mod_path = ModPath::from_src(ast_path, &Hygiene::new_unhygienic()).unwrap();
let mod_path = ModPath::from_src(&db, ast_path, &Hygiene::new_unhygienic()).unwrap();

let def_map = module.def_map(&db);
let resolved = def_map
Expand Down
2 changes: 1 addition & 1 deletion crates/hir_def/src/item_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl ItemTree {
let mut item_tree = match_ast! {
match syntax {
ast::SourceFile(file) => {
top_attrs = Some(RawAttrs::new(&file, &hygiene));
top_attrs = Some(RawAttrs::new(db, &file, &hygiene));
ctx.lower_module_items(&file)
},
ast::MacroItems(items) => {
Expand Down
34 changes: 19 additions & 15 deletions crates/hir_def/src/item_tree/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,20 @@ where
}
}

pub(super) struct Ctx {
pub(super) struct Ctx<'a> {
db: &'a dyn DefDatabase,
tree: ItemTree,
hygiene: Hygiene,
file: HirFileId,
source_ast_id_map: Arc<AstIdMap>,
body_ctx: crate::body::LowerCtx,
body_ctx: crate::body::LowerCtx<'a>,
forced_visibility: Option<RawVisibilityId>,
}

impl Ctx {
pub(super) fn new(db: &dyn DefDatabase, hygiene: Hygiene, file: HirFileId) -> Self {
impl<'a> Ctx<'a> {
pub(super) fn new(db: &'a dyn DefDatabase, hygiene: Hygiene, file: HirFileId) -> Self {
Self {
db,
tree: ItemTree::default(),
hygiene,
file,
Expand Down Expand Up @@ -126,7 +128,7 @@ impl Ctx {
| ast::Item::MacroDef(_) => {}
};

let attrs = RawAttrs::new(item, &self.hygiene);
let attrs = RawAttrs::new(self.db, item, &self.hygiene);
let items = match item {
ast::Item::Struct(ast) => self.lower_struct(ast).map(Into::into),
ast::Item::Union(ast) => self.lower_union(ast).map(Into::into),
Expand Down Expand Up @@ -256,7 +258,7 @@ impl Ctx {
for field in fields.fields() {
if let Some(data) = self.lower_record_field(&field) {
let idx = self.data().fields.alloc(data);
self.add_attrs(idx.into(), RawAttrs::new(&field, &self.hygiene));
self.add_attrs(idx.into(), RawAttrs::new(self.db, &field, &self.hygiene));
}
}
let end = self.next_field_idx();
Expand All @@ -276,7 +278,7 @@ impl Ctx {
for (i, field) in fields.fields().enumerate() {
let data = self.lower_tuple_field(i, &field);
let idx = self.data().fields.alloc(data);
self.add_attrs(idx.into(), RawAttrs::new(&field, &self.hygiene));
self.add_attrs(idx.into(), RawAttrs::new(self.db, &field, &self.hygiene));
}
let end = self.next_field_idx();
IdRange::new(start..end)
Expand Down Expand Up @@ -321,7 +323,7 @@ impl Ctx {
for variant in variants.variants() {
if let Some(data) = self.lower_variant(&variant) {
let idx = self.data().variants.alloc(data);
self.add_attrs(idx.into(), RawAttrs::new(&variant, &self.hygiene));
self.add_attrs(idx.into(), RawAttrs::new(self.db, &variant, &self.hygiene));
}
}
let end = self.next_variant_idx();
Expand Down Expand Up @@ -364,7 +366,7 @@ impl Ctx {
};
let ty = Interned::new(self_type);
let idx = self.data().params.alloc(Param::Normal(ty));
self.add_attrs(idx.into(), RawAttrs::new(&self_param, &self.hygiene));
self.add_attrs(idx.into(), RawAttrs::new(self.db, &self_param, &self.hygiene));
has_self_param = true;
}
for param in param_list.params() {
Expand All @@ -376,7 +378,7 @@ impl Ctx {
self.data().params.alloc(Param::Normal(ty))
}
};
self.add_attrs(idx.into(), RawAttrs::new(&param, &self.hygiene));
self.add_attrs(idx.into(), RawAttrs::new(self.db, &param, &self.hygiene));
}
}
let end_param = self.next_param_idx();
Expand Down Expand Up @@ -522,10 +524,11 @@ impl Ctx {
let is_unsafe = trait_def.unsafe_token().is_some();
let bounds = self.lower_type_bounds(trait_def);
let items = trait_def.assoc_item_list().map(|list| {
let db = self.db;
self.with_inherited_visibility(visibility, |this| {
list.assoc_items()
.filter_map(|item| {
let attrs = RawAttrs::new(&item, &this.hygiene);
let attrs = RawAttrs::new(db, &item, &this.hygiene);
this.collect_inner_items(item.syntax());
this.lower_assoc_item(&item).map(|item| {
this.add_attrs(ModItem::from(item).into(), attrs);
Expand Down Expand Up @@ -567,7 +570,7 @@ impl Ctx {
.filter_map(|item| {
self.collect_inner_items(item.syntax());
let assoc = self.lower_assoc_item(&item)?;
let attrs = RawAttrs::new(&item, &self.hygiene);
let attrs = RawAttrs::new(self.db, &item, &self.hygiene);
self.add_attrs(ModItem::from(assoc).into(), attrs);
Some(assoc)
})
Expand All @@ -585,6 +588,7 @@ impl Ctx {
let mut imports = Vec::new();
let tree = self.tree.data_mut();
ModPath::expand_use_item(
self.db,
InFile::new(self.file, use_item.clone()),
&self.hygiene,
|path, _use_tree, is_glob, alias| {
Expand Down Expand Up @@ -618,7 +622,7 @@ impl Ctx {
}

fn lower_macro_call(&mut self, m: &ast::MacroCall) -> Option<FileItemTreeId<MacroCall>> {
let path = Interned::new(ModPath::from_src(m.path()?, &self.hygiene)?);
let path = Interned::new(ModPath::from_src(self.db, m.path()?, &self.hygiene)?);
let ast_id = self.source_ast_id_map.ast_id(m);
let res = MacroCall { path, ast_id };
Some(id(self.data().macro_calls.alloc(res)))
Expand Down Expand Up @@ -647,7 +651,7 @@ impl Ctx {
list.extern_items()
.filter_map(|item| {
self.collect_inner_items(item.syntax());
let attrs = RawAttrs::new(&item, &self.hygiene);
let attrs = RawAttrs::new(self.db, &item, &self.hygiene);
let id: ModItem = match item {
ast::ExternItem::Fn(ast) => {
let func_id = self.lower_function(&ast)?;
Expand Down Expand Up @@ -755,7 +759,7 @@ impl Ctx {
fn lower_visibility(&mut self, item: &impl ast::VisibilityOwner) -> RawVisibilityId {
let vis = match self.forced_visibility {
Some(vis) => return vis,
None => RawVisibility::from_ast_with_hygiene(item.visibility(), &self.hygiene),
None => RawVisibility::from_ast_with_hygiene(self.db, item.visibility(), &self.hygiene),
};

self.data().vis.alloc(vis)
Expand Down
4 changes: 2 additions & 2 deletions crates/hir_def/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ impl AsMacroCall for InFile<&ast::MacroCall> {
) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro> {
let ast_id = AstId::new(self.file_id, db.ast_id_map(self.file_id).ast_id(self.value));
let h = Hygiene::new(db.upcast(), self.file_id);
let path = self.value.path().and_then(|path| path::ModPath::from_src(path, &h));
let path = self.value.path().and_then(|path| path::ModPath::from_src(db, path, &h));

let path = match error_sink
.option(path, || mbe::ExpandError::Other("malformed macro invocation".into()))
Expand Down Expand Up @@ -712,7 +712,7 @@ fn macro_call_as_call_id(
krate,
macro_call,
def,
&|path: ast::Path| resolver(path::ModPath::from_src(path, &hygiene)?),
&|path: ast::Path| resolver(path::ModPath::from_src(db, path, &hygiene)?),
error_sink,
)
.map(MacroCallId::from)
Expand Down
1 change: 1 addition & 0 deletions crates/hir_def/src/nameres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ mod diagnostics {
let mut cur = 0;
let mut tree = None;
ModPath::expand_use_item(
db,
InFile::new(ast.file_id, use_item),
&hygiene,
|_mod_path, use_tree, _is_glob, _alias| {
Expand Down
Loading