Skip to content

Commit

Permalink
Auto merge of rust-lang#120112 - matthiaskrgr:rollup-48o3919, r=matth…
Browse files Browse the repository at this point in the history
…iaskrgr

Rollup of 9 pull requests

Successful merges:

 - rust-lang#119582 (bootstrap: handle vendored sources when remapping crate paths)
 - rust-lang#119730 (docs: fix typos)
 - rust-lang#119828 (Improved collapse_debuginfo attribute, added command-line flag)
 - rust-lang#119869 (replace `track_errors` usages with bubbling up `ErrorGuaranteed`)
 - rust-lang#120037 (Remove `next_root_ty_var`)
 - rust-lang#120094 (tests/ui/asm/inline-syntax: adapt for LLVM 18)
 - rust-lang#120096 (Set RUSTC_BOOTSTRAP=1 consistently)
 - rust-lang#120101 (change `.unwrap()` to `?` on write where `fmt::Result` is returned)
 - rust-lang#120102 (Fix typo in munmap_partial.rs)

r? `@ghost`
`@rustbot` modify labels: rollup
  • Loading branch information
bors committed Jan 19, 2024
2 parents 16fadb3 + 9c6795b commit 92d7277
Show file tree
Hide file tree
Showing 61 changed files with 771 additions and 255 deletions.
3 changes: 3 additions & 0 deletions compiler/rustc_expand/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ expand_attributes_wrong_form =
expand_cannot_be_name_of_macro =
`{$trait_ident}` cannot be a name of {$macro_type} macro
expand_collapse_debuginfo_illegal =
illegal value for attribute #[collapse_debuginfo(no|external|yes)]
expand_count_repetition_misplaced =
`count` can not be placed inside the inner-most repetition
Expand Down
56 changes: 54 additions & 2 deletions compiler/rustc_expand/src/base.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![deny(rustc::untranslatable_diagnostic)]

use crate::base::ast::NestedMetaItem;
use crate::errors;
use crate::expand::{self, AstFragment, Invocation};
use crate::module::DirOwnership;
Expand All @@ -19,6 +20,7 @@ use rustc_feature::Features;
use rustc_lint_defs::builtin::PROC_MACRO_BACK_COMPAT;
use rustc_lint_defs::{BufferedEarlyLint, BuiltinLintDiagnostics, RegisteredTools};
use rustc_parse::{parser, MACRO_ARGUMENTS};
use rustc_session::config::CollapseMacroDebuginfo;
use rustc_session::errors::report_lit_error;
use rustc_session::{parse::ParseSess, Limit, Session};
use rustc_span::def_id::{CrateNum, DefId, LocalDefId};
Expand Down Expand Up @@ -761,6 +763,55 @@ impl SyntaxExtension {
}
}

fn collapse_debuginfo_by_name(sess: &Session, attr: &Attribute) -> CollapseMacroDebuginfo {
use crate::errors::CollapseMacroDebuginfoIllegal;
// #[collapse_debuginfo] without enum value (#[collapse_debuginfo(no/external/yes)])
// considered as `yes`
attr.meta_item_list().map_or(CollapseMacroDebuginfo::Yes, |l| {
let [NestedMetaItem::MetaItem(item)] = &l[..] else {
sess.dcx().emit_err(CollapseMacroDebuginfoIllegal { span: attr.span });
return CollapseMacroDebuginfo::Unspecified;
};
if !item.is_word() {
sess.dcx().emit_err(CollapseMacroDebuginfoIllegal { span: item.span });
CollapseMacroDebuginfo::Unspecified
} else {
match item.name_or_empty() {
sym::no => CollapseMacroDebuginfo::No,
sym::external => CollapseMacroDebuginfo::External,
sym::yes => CollapseMacroDebuginfo::Yes,
_ => {
sess.dcx().emit_err(CollapseMacroDebuginfoIllegal { span: item.span });
CollapseMacroDebuginfo::Unspecified
}
}
}
})
}

/// if-ext - if macro from different crate (related to callsite code)
/// | cmd \ attr | no | (unspecified) | external | yes |
/// | no | no | no | no | no |
/// | (unspecified) | no | no | if-ext | yes |
/// | external | no | if-ext | if-ext | yes |
/// | yes | yes | yes | yes | yes |
fn get_collapse_debuginfo(sess: &Session, attrs: &[ast::Attribute], is_local: bool) -> bool {
let collapse_debuginfo_attr = attr::find_by_name(attrs, sym::collapse_debuginfo)
.map(|v| Self::collapse_debuginfo_by_name(sess, v))
.unwrap_or(CollapseMacroDebuginfo::Unspecified);
let flag = sess.opts.unstable_opts.collapse_macro_debuginfo;
let attr = collapse_debuginfo_attr;
let ext = !is_local;
#[rustfmt::skip]
let collapse_table = [
[false, false, false, false],
[false, false, ext, true],
[false, ext, ext, true],
[true, true, true, true],
];
collapse_table[flag as usize][attr as usize]
}

/// Constructs a syntax extension with the given properties
/// and other properties converted from attributes.
pub fn new(
Expand All @@ -772,6 +823,7 @@ impl SyntaxExtension {
edition: Edition,
name: Symbol,
attrs: &[ast::Attribute],
is_local: bool,
) -> SyntaxExtension {
let allow_internal_unstable =
attr::allow_internal_unstable(sess, attrs).collect::<Vec<Symbol>>();
Expand All @@ -780,8 +832,8 @@ impl SyntaxExtension {
let local_inner_macros = attr::find_by_name(attrs, sym::macro_export)
.and_then(|macro_export| macro_export.meta_item_list())
.is_some_and(|l| attr::list_contains_name(&l, sym::local_inner_macros));
let collapse_debuginfo = attr::contains_name(attrs, sym::collapse_debuginfo);
tracing::debug!(?local_inner_macros, ?collapse_debuginfo, ?allow_internal_unsafe);
let collapse_debuginfo = Self::get_collapse_debuginfo(sess, attrs, is_local);
tracing::debug!(?name, ?local_inner_macros, ?collapse_debuginfo, ?allow_internal_unsafe);

let (builtin_name, helper_attrs) = attr::find_by_name(attrs, sym::rustc_builtin_macro)
.map(|attr| {
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_expand/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ pub(crate) struct ResolveRelativePath {
pub path: String,
}

#[derive(Diagnostic)]
#[diag(expand_collapse_debuginfo_illegal)]
pub(crate) struct CollapseMacroDebuginfoIllegal {
#[primary_span]
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(expand_macro_const_stability)]
pub(crate) struct MacroConstStability {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_expand/src/mbe/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ pub fn compile_declarative_macro(
edition,
def.ident.name,
&def.attrs,
def.id != DUMMY_NODE_ID,
)
};
let dummy_syn_ext = || (mk_syn_ext(Box::new(macro_rules_dummy_expander)), Vec::new());
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_feature/src/builtin_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[

// `#[collapse_debuginfo]`
gated!(
collapse_debuginfo, Normal, template!(Word), WarnFollowing,
collapse_debuginfo, Normal, template!(Word, List: "no|external|yes"), ErrorFollowing,
experimental!(collapse_debuginfo)
),

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/astconv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1446,7 +1446,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
}

let candidates: Vec<_> = tcx
.inherent_impls(adt_did)
.inherent_impls(adt_did)?
.iter()
.filter_map(|&impl_| Some((impl_, self.lookup_assoc_ty_unchecked(name, block, impl_)?)))
.collect();
Expand Down
79 changes: 48 additions & 31 deletions compiler/rustc_hir_analysis/src/coherence/inherent_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,41 @@ use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_middle::ty::fast_reject::{simplify_type, SimplifiedType, TreatParams};
use rustc_middle::ty::{self, CrateInherentImpls, Ty, TyCtxt};
use rustc_span::symbol::sym;
use rustc_span::ErrorGuaranteed;

use crate::errors;

/// On-demand query: yields a map containing all types mapped to their inherent impls.
pub fn crate_inherent_impls(tcx: TyCtxt<'_>, (): ()) -> CrateInherentImpls {
pub fn crate_inherent_impls(
tcx: TyCtxt<'_>,
(): (),
) -> Result<&'_ CrateInherentImpls, ErrorGuaranteed> {
let mut collect = InherentCollect { tcx, impls_map: Default::default() };
let mut res = Ok(());
for id in tcx.hir().items() {
collect.check_item(id);
res = res.and(collect.check_item(id));
}
collect.impls_map
res?;
Ok(tcx.arena.alloc(collect.impls_map))
}

pub fn crate_incoherent_impls(tcx: TyCtxt<'_>, simp: SimplifiedType) -> &[DefId] {
let crate_map = tcx.crate_inherent_impls(());
tcx.arena.alloc_from_iter(
pub fn crate_incoherent_impls(
tcx: TyCtxt<'_>,
simp: SimplifiedType,
) -> Result<&[DefId], ErrorGuaranteed> {
let crate_map = tcx.crate_inherent_impls(())?;
Ok(tcx.arena.alloc_from_iter(
crate_map.incoherent_impls.get(&simp).unwrap_or(&Vec::new()).iter().map(|d| d.to_def_id()),
)
))
}

/// On-demand query: yields a vector of the inherent impls for a specific type.
pub fn inherent_impls(tcx: TyCtxt<'_>, ty_def_id: LocalDefId) -> &[DefId] {
let crate_map = tcx.crate_inherent_impls(());
match crate_map.inherent_impls.get(&ty_def_id) {
pub fn inherent_impls(tcx: TyCtxt<'_>, ty_def_id: LocalDefId) -> Result<&[DefId], ErrorGuaranteed> {
let crate_map = tcx.crate_inherent_impls(())?;
Ok(match crate_map.inherent_impls.get(&ty_def_id) {
Some(v) => &v[..],
None => &[],
}
})
}

struct InherentCollect<'tcx> {
Expand All @@ -47,33 +56,36 @@ struct InherentCollect<'tcx> {
}

impl<'tcx> InherentCollect<'tcx> {
fn check_def_id(&mut self, impl_def_id: LocalDefId, self_ty: Ty<'tcx>, ty_def_id: DefId) {
fn check_def_id(
&mut self,
impl_def_id: LocalDefId,
self_ty: Ty<'tcx>,
ty_def_id: DefId,
) -> Result<(), ErrorGuaranteed> {
if let Some(ty_def_id) = ty_def_id.as_local() {
// Add the implementation to the mapping from implementation to base
// type def ID, if there is a base type for this implementation and
// the implementation does not have any associated traits.
let vec = self.impls_map.inherent_impls.entry(ty_def_id).or_default();
vec.push(impl_def_id.to_def_id());
return;
return Ok(());
}

if self.tcx.features().rustc_attrs {
let items = self.tcx.associated_item_def_ids(impl_def_id);

if !self.tcx.has_attr(ty_def_id, sym::rustc_has_incoherent_inherent_impls) {
let impl_span = self.tcx.def_span(impl_def_id);
self.tcx.dcx().emit_err(errors::InherentTyOutside { span: impl_span });
return;
return Err(self.tcx.dcx().emit_err(errors::InherentTyOutside { span: impl_span }));
}

for &impl_item in items {
if !self.tcx.has_attr(impl_item, sym::rustc_allow_incoherent_impl) {
let impl_span = self.tcx.def_span(impl_def_id);
self.tcx.dcx().emit_err(errors::InherentTyOutsideRelevant {
return Err(self.tcx.dcx().emit_err(errors::InherentTyOutsideRelevant {
span: impl_span,
help_span: self.tcx.def_span(impl_item),
});
return;
}));
}
}

Expand All @@ -82,24 +94,28 @@ impl<'tcx> InherentCollect<'tcx> {
} else {
bug!("unexpected self type: {:?}", self_ty);
}
Ok(())
} else {
let impl_span = self.tcx.def_span(impl_def_id);
self.tcx.dcx().emit_err(errors::InherentTyOutsideNew { span: impl_span });
Err(self.tcx.dcx().emit_err(errors::InherentTyOutsideNew { span: impl_span }))
}
}

fn check_primitive_impl(&mut self, impl_def_id: LocalDefId, ty: Ty<'tcx>) {
fn check_primitive_impl(
&mut self,
impl_def_id: LocalDefId,
ty: Ty<'tcx>,
) -> Result<(), ErrorGuaranteed> {
let items = self.tcx.associated_item_def_ids(impl_def_id);
if !self.tcx.hir().rustc_coherence_is_core() {
if self.tcx.features().rustc_attrs {
for &impl_item in items {
if !self.tcx.has_attr(impl_item, sym::rustc_allow_incoherent_impl) {
let span = self.tcx.def_span(impl_def_id);
self.tcx.dcx().emit_err(errors::InherentTyOutsidePrimitive {
return Err(self.tcx.dcx().emit_err(errors::InherentTyOutsidePrimitive {
span,
help_span: self.tcx.def_span(impl_item),
});
return;
}));
}
}
} else {
Expand All @@ -108,8 +124,7 @@ impl<'tcx> InherentCollect<'tcx> {
if let ty::Ref(_, subty, _) = ty.kind() {
note = Some(errors::InherentPrimitiveTyNote { subty: *subty });
}
self.tcx.dcx().emit_err(errors::InherentPrimitiveTy { span, note });
return;
return Err(self.tcx.dcx().emit_err(errors::InherentPrimitiveTy { span, note }));
}
}

Expand All @@ -118,11 +133,12 @@ impl<'tcx> InherentCollect<'tcx> {
} else {
bug!("unexpected primitive type: {:?}", ty);
}
Ok(())
}

fn check_item(&mut self, id: hir::ItemId) {
fn check_item(&mut self, id: hir::ItemId) -> Result<(), ErrorGuaranteed> {
if !matches!(self.tcx.def_kind(id.owner_id), DefKind::Impl { of_trait: false }) {
return;
return Ok(());
}

let id = id.owner_id.def_id;
Expand All @@ -132,10 +148,10 @@ impl<'tcx> InherentCollect<'tcx> {
ty::Adt(def, _) => self.check_def_id(id, self_ty, def.did()),
ty::Foreign(did) => self.check_def_id(id, self_ty, did),
ty::Dynamic(data, ..) if data.principal_def_id().is_some() => {
self.check_def_id(id, self_ty, data.principal_def_id().unwrap());
self.check_def_id(id, self_ty, data.principal_def_id().unwrap())
}
ty::Dynamic(..) => {
self.tcx.dcx().emit_err(errors::InherentDyn { span: item_span });
Err(self.tcx.dcx().emit_err(errors::InherentDyn { span: item_span }))
}
ty::Bool
| ty::Char
Expand All @@ -151,7 +167,7 @@ impl<'tcx> InherentCollect<'tcx> {
| ty::FnPtr(_)
| ty::Tuple(..) => self.check_primitive_impl(id, self_ty),
ty::Alias(..) | ty::Param(_) => {
self.tcx.dcx().emit_err(errors::InherentNominal { span: item_span });
Err(self.tcx.dcx().emit_err(errors::InherentNominal { span: item_span }))
}
ty::FnDef(..)
| ty::Closure(..)
Expand All @@ -162,7 +178,8 @@ impl<'tcx> InherentCollect<'tcx> {
| ty::Infer(_) => {
bug!("unexpected impl self type of impl: {:?} {:?}", id, self_ty);
}
ty::Error(_) => {}
// We could bail out here, but that will silence other useful errors.
ty::Error(_) => Ok(()),
}
}
}
Loading

0 comments on commit 92d7277

Please sign in to comment.