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
8 changes: 4 additions & 4 deletions crates/cfg/src/cfg_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ fn next_cfg_expr<S: Copy>(it: &mut tt::iter::TtIter<'_, S>) -> Option<CfgExpr> {
};

// Eat comma separator
if let Some(TtElement::Leaf(tt::Leaf::Punct(punct))) = it.peek() {
if punct.char == ',' {
it.next();
}
if let Some(TtElement::Leaf(tt::Leaf::Punct(punct))) = it.peek()
&& punct.char == ','
{
it.next();
}
Some(ret)
}
Expand Down
8 changes: 4 additions & 4 deletions crates/hir-def/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,10 +377,10 @@ fn parse_repr_tt(tt: &crate::tt::TopSubtree) -> Option<ReprOptions> {
let mut align = None;
if let Some(TtElement::Subtree(_, mut tt_iter)) = tts.peek() {
tts.next();
if let Some(TtElement::Leaf(tt::Leaf::Literal(lit))) = tt_iter.next() {
if let Ok(a) = lit.symbol.as_str().parse() {
align = Align::from_bytes(a).ok();
}
if let Some(TtElement::Leaf(tt::Leaf::Literal(lit))) = tt_iter.next()
&& let Ok(a) = lit.symbol.as_str().parse()
{
align = Align::from_bytes(a).ok();
}
}
ReprOptions { align, ..Default::default() }
Expand Down
39 changes: 19 additions & 20 deletions crates/hir-def/src/expr_store/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1487,13 +1487,13 @@ impl ExprCollector<'_> {
ast::Expr::UnderscoreExpr(_) => self.alloc_pat_from_expr(Pat::Wild, syntax_ptr),
ast::Expr::ParenExpr(e) => {
// We special-case `(..)` for consistency with patterns.
if let Some(ast::Expr::RangeExpr(range)) = e.expr() {
if range.is_range_full() {
return Some(self.alloc_pat_from_expr(
Pat::Tuple { args: Box::default(), ellipsis: Some(0) },
syntax_ptr,
));
}
if let Some(ast::Expr::RangeExpr(range)) = e.expr()
&& range.is_range_full()
{
return Some(self.alloc_pat_from_expr(
Pat::Tuple { args: Box::default(), ellipsis: Some(0) },
syntax_ptr,
));
}
return e.expr().and_then(|expr| self.maybe_collect_expr_as_pat(&expr));
}
Expand Down Expand Up @@ -2569,19 +2569,18 @@ impl ExprCollector<'_> {
}
}
RibKind::MacroDef(macro_id) => {
if let Some((parent_ctx, label_macro_id)) = hygiene_info {
if label_macro_id == **macro_id {
// A macro is allowed to refer to labels from before its declaration.
// Therefore, if we got to the rib of its declaration, give up its hygiene
// and use its parent expansion.

hygiene_id =
HygieneId::new(parent_ctx.opaque_and_semitransparent(self.db));
hygiene_info = parent_ctx.outer_expn(self.db).map(|expansion| {
let expansion = self.db.lookup_intern_macro_call(expansion.into());
(parent_ctx.parent(self.db), expansion.def)
});
}
if let Some((parent_ctx, label_macro_id)) = hygiene_info
&& label_macro_id == **macro_id
{
// A macro is allowed to refer to labels from before its declaration.
// Therefore, if we got to the rib of its declaration, give up its hygiene
// and use its parent expansion.

hygiene_id = HygieneId::new(parent_ctx.opaque_and_semitransparent(self.db));
hygiene_info = parent_ctx.outer_expn(self.db).map(|expansion| {
let expansion = self.db.lookup_intern_macro_call(expansion.into());
(parent_ctx.parent(self.db), expansion.def)
});
}
}
_ => {}
Expand Down
8 changes: 4 additions & 4 deletions crates/hir-def/src/expr_store/lower/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,10 @@ impl ExprCollector<'_> {
}
};

if let Some(operand_idx) = operand_idx {
if let Some(position_span) = to_span(arg.position_span) {
mappings.push((position_span, operand_idx));
}
if let Some(operand_idx) = operand_idx
&& let Some(position_span) = to_span(arg.position_span)
{
mappings.push((position_span, operand_idx));
}
}
}
Expand Down
21 changes: 11 additions & 10 deletions crates/hir-def/src/expr_store/lower/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,16 +211,17 @@ pub(super) fn lower_path(
// Basically, even in rustc it is quite hacky:
// https://github.com/rust-lang/rust/blob/614f273e9388ddd7804d5cbc80b8865068a3744e/src/librustc_resolve/macros.rs#L456
// We follow what it did anyway :)
if segments.len() == 1 && kind == PathKind::Plain {
if let Some(_macro_call) = path.syntax().parent().and_then(ast::MacroCall::cast) {
let syn_ctxt = collector.expander.ctx_for_range(path.segment()?.syntax().text_range());
if let Some(macro_call_id) = syn_ctxt.outer_expn(collector.db) {
if collector.db.lookup_intern_macro_call(macro_call_id.into()).def.local_inner {
kind = match resolve_crate_root(collector.db, syn_ctxt) {
Some(crate_root) => PathKind::DollarCrate(crate_root),
None => PathKind::Crate,
}
}
if segments.len() == 1
&& kind == PathKind::Plain
&& let Some(_macro_call) = path.syntax().parent().and_then(ast::MacroCall::cast)
{
let syn_ctxt = collector.expander.ctx_for_range(path.segment()?.syntax().text_range());
if let Some(macro_call_id) = syn_ctxt.outer_expn(collector.db)
&& collector.db.lookup_intern_macro_call(macro_call_id.into()).def.local_inner
{
kind = match resolve_crate_root(collector.db, syn_ctxt) {
Some(crate_root) => PathKind::DollarCrate(crate_root),
None => PathKind::Crate,
}
}
}
Expand Down
12 changes: 5 additions & 7 deletions crates/hir-def/src/expr_store/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -900,14 +900,12 @@ impl Printer<'_> {
let field_name = arg.name.display(self.db, edition).to_string();

let mut same_name = false;
if let Pat::Bind { id, subpat: None } = &self.store[arg.pat] {
if let Binding { name, mode: BindingAnnotation::Unannotated, .. } =
if let Pat::Bind { id, subpat: None } = &self.store[arg.pat]
&& let Binding { name, mode: BindingAnnotation::Unannotated, .. } =
&self.store.assert_expr_only().bindings[*id]
{
if name.as_str() == field_name {
same_name = true;
}
}
&& name.as_str() == field_name
{
same_name = true;
}

w!(p, "{}", field_name);
Expand Down
72 changes: 36 additions & 36 deletions crates/hir-def/src/find_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ struct FindPathCtx<'db> {
/// Attempts to find a path to refer to the given `item` visible from the `from` ModuleId
fn find_path_inner(ctx: &FindPathCtx<'_>, item: ItemInNs, max_len: usize) -> Option<ModPath> {
// - if the item is a module, jump straight to module search
if !ctx.is_std_item {
if let ItemInNs::Types(ModuleDefId::ModuleId(module_id)) = item {
return find_path_for_module(ctx, &mut FxHashSet::default(), module_id, true, max_len)
.map(|choice| choice.path);
}
if !ctx.is_std_item
&& let ItemInNs::Types(ModuleDefId::ModuleId(module_id)) = item
{
return find_path_for_module(ctx, &mut FxHashSet::default(), module_id, true, max_len)
.map(|choice| choice.path);
}

let may_be_in_scope = match ctx.prefix {
Expand Down Expand Up @@ -226,15 +226,15 @@ fn find_path_for_module(
}

// - if the module can be referenced as self, super or crate, do that
if let Some(kind) = is_kw_kind_relative_to_from(ctx.from_def_map, module_id, ctx.from) {
if ctx.prefix != PrefixKind::ByCrate || kind == PathKind::Crate {
return Some(Choice {
path: ModPath::from_segments(kind, None),
path_text_len: path_kind_len(kind),
stability: Stable,
prefer_due_to_prelude: false,
});
}
if let Some(kind) = is_kw_kind_relative_to_from(ctx.from_def_map, module_id, ctx.from)
&& (ctx.prefix != PrefixKind::ByCrate || kind == PathKind::Crate)
{
return Some(Choice {
path: ModPath::from_segments(kind, None),
path_text_len: path_kind_len(kind),
stability: Stable,
prefer_due_to_prelude: false,
});
}

// - if the module is in the prelude, return it by that path
Expand Down Expand Up @@ -604,29 +604,29 @@ fn find_local_import_locations(
&def_map[module.local_id]
};

if let Some((name, vis, declared)) = data.scope.name_of(item) {
if vis.is_visible_from(db, from) {
let is_pub_or_explicit = match vis {
Visibility::Module(_, VisibilityExplicitness::Explicit) => {
cov_mark::hit!(explicit_private_imports);
true
}
Visibility::Module(_, VisibilityExplicitness::Implicit) => {
cov_mark::hit!(discount_private_imports);
false
}
Visibility::PubCrate(_) => true,
Visibility::Public => true,
};

// Ignore private imports unless they are explicit. these could be used if we are
// in a submodule of this module, but that's usually not
// what the user wants; and if this module can import
// the item and we're a submodule of it, so can we.
// Also this keeps the cached data smaller.
if declared || is_pub_or_explicit {
cb(visited_modules, name, module);
if let Some((name, vis, declared)) = data.scope.name_of(item)
&& vis.is_visible_from(db, from)
{
let is_pub_or_explicit = match vis {
Visibility::Module(_, VisibilityExplicitness::Explicit) => {
cov_mark::hit!(explicit_private_imports);
true
}
Visibility::Module(_, VisibilityExplicitness::Implicit) => {
cov_mark::hit!(discount_private_imports);
false
}
Visibility::PubCrate(_) => true,
Visibility::Public => true,
};

// Ignore private imports unless they are explicit. these could be used if we are
// in a submodule of this module, but that's usually not
// what the user wants; and if this module can import
// the item and we're a submodule of it, so can we.
// Also this keeps the cached data smaller.
if declared || is_pub_or_explicit {
cb(visited_modules, name, module);
}
}

Expand Down
9 changes: 4 additions & 5 deletions crates/hir-def/src/item_scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,12 +510,11 @@ impl ItemScope {
id: AttrId,
idx: usize,
) {
if let Some(derives) = self.derive_macros.get_mut(&adt) {
if let Some(DeriveMacroInvocation { derive_call_ids, .. }) =
if let Some(derives) = self.derive_macros.get_mut(&adt)
&& let Some(DeriveMacroInvocation { derive_call_ids, .. }) =
derives.iter_mut().find(|&&mut DeriveMacroInvocation { attr_id, .. }| id == attr_id)
{
derive_call_ids[idx] = Some(call);
}
{
derive_call_ids[idx] = Some(call);
}
}

Expand Down
23 changes: 11 additions & 12 deletions crates/hir-def/src/item_tree/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ impl<'a> Ctx<'a> {
.flat_map(|item| self.lower_mod_item(&item))
.collect();

if let Some(ast::Expr::MacroExpr(tail_macro)) = stmts.expr() {
if let Some(call) = tail_macro.macro_call() {
cov_mark::hit!(macro_stmt_with_trailing_macro_expr);
if let Some(mod_item) = self.lower_mod_item(&call.into()) {
self.top_level.push(mod_item);
}
if let Some(ast::Expr::MacroExpr(tail_macro)) = stmts.expr()
&& let Some(call) = tail_macro.macro_call()
{
cov_mark::hit!(macro_stmt_with_trailing_macro_expr);
if let Some(mod_item) = self.lower_mod_item(&call.into()) {
self.top_level.push(mod_item);
}
}

Expand All @@ -112,12 +112,11 @@ impl<'a> Ctx<'a> {
_ => None,
})
.collect();
if let Some(ast::Expr::MacroExpr(expr)) = block.tail_expr() {
if let Some(call) = expr.macro_call() {
if let Some(mod_item) = self.lower_mod_item(&call.into()) {
self.top_level.push(mod_item);
}
}
if let Some(ast::Expr::MacroExpr(expr)) = block.tail_expr()
&& let Some(call) = expr.macro_call()
&& let Some(mod_item) = self.lower_mod_item(&call.into())
{
self.top_level.push(mod_item);
}
self.tree.vis.arena = self.visibilities.into_iter().collect();
self.tree.top_level = self.top_level.into_boxed_slice();
Expand Down
8 changes: 4 additions & 4 deletions crates/hir-def/src/lang_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,10 @@ pub(crate) fn crate_notable_traits(db: &dyn DefDatabase, krate: Crate) -> Option

for (_, module_data) in crate_def_map.modules() {
for def in module_data.scope.declarations() {
if let ModuleDefId::TraitId(trait_) = def {
if db.attrs(trait_.into()).has_doc_notable_trait() {
traits.push(trait_);
}
if let ModuleDefId::TraitId(trait_) = def
&& db.attrs(trait_.into()).has_doc_notable_trait()
{
traits.push(trait_);
}
}
}
Expand Down
64 changes: 30 additions & 34 deletions crates/hir-def/src/macro_expansion_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,46 +221,42 @@ pub fn identity_when_valid(_attr: TokenStream, item: TokenStream) -> TokenStream
_ => None,
};

if let Some(src) = src {
if let Some(file_id) = src.file_id.macro_file() {
if let MacroKind::Derive
| MacroKind::DeriveBuiltIn
| MacroKind::Attr
| MacroKind::AttrBuiltIn = file_id.kind(&db)
{
let call = file_id.call_node(&db);
let mut show_spans = false;
let mut show_ctxt = false;
for comment in
call.value.children_with_tokens().filter(|it| it.kind() == COMMENT)
{
show_spans |= comment.to_string().contains("+spans");
show_ctxt |= comment.to_string().contains("+syntaxctxt");
}
let pp = pretty_print_macro_expansion(
src.value,
db.span_map(src.file_id).as_ref(),
show_spans,
show_ctxt,
);
format_to!(expanded_text, "\n{}", pp)
}
if let Some(src) = src
&& let Some(file_id) = src.file_id.macro_file()
&& let MacroKind::Derive
| MacroKind::DeriveBuiltIn
| MacroKind::Attr
| MacroKind::AttrBuiltIn = file_id.kind(&db)
{
let call = file_id.call_node(&db);
let mut show_spans = false;
let mut show_ctxt = false;
for comment in call.value.children_with_tokens().filter(|it| it.kind() == COMMENT) {
show_spans |= comment.to_string().contains("+spans");
show_ctxt |= comment.to_string().contains("+syntaxctxt");
}
let pp = pretty_print_macro_expansion(
src.value,
db.span_map(src.file_id).as_ref(),
show_spans,
show_ctxt,
);
format_to!(expanded_text, "\n{}", pp)
}
}

for impl_id in def_map[local_id].scope.impls() {
let src = impl_id.lookup(&db).source(&db);
if let Some(macro_file) = src.file_id.macro_file() {
if let MacroKind::DeriveBuiltIn | MacroKind::Derive = macro_file.kind(&db) {
let pp = pretty_print_macro_expansion(
src.value.syntax().clone(),
db.span_map(macro_file.into()).as_ref(),
false,
false,
);
format_to!(expanded_text, "\n{}", pp)
}
if let Some(macro_file) = src.file_id.macro_file()
&& let MacroKind::DeriveBuiltIn | MacroKind::Derive = macro_file.kind(&db)
{
let pp = pretty_print_macro_expansion(
src.value.syntax().clone(),
db.span_map(macro_file.into()).as_ref(),
false,
false,
);
format_to!(expanded_text, "\n{}", pp)
}
}

Expand Down
Loading
Loading