Skip to content

Rollup of 5 pull requests #70483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Mar 28, 2020
16 changes: 11 additions & 5 deletions src/librustc/hir/map/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
// Make sure that the DepNode of some node coincides with the HirId
// owner of that node.
if cfg!(debug_assertions) {
let node_id = self.definitions.hir_to_node_id(hir_id);
assert_eq!(self.definitions.node_to_hir_id(node_id), hir_id);
let node_id = self.definitions.hir_id_to_node_id(hir_id);
assert_eq!(self.definitions.node_id_to_hir_id(node_id), hir_id);

if hir_id.owner != self.current_dep_node_owner {
let node_str = match self.definitions.opt_local_def_id(node_id) {
Expand Down Expand Up @@ -342,7 +342,9 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
debug!("visit_item: {:?}", i);
debug_assert_eq!(
i.hir_id.owner,
self.definitions.opt_local_def_id(self.definitions.hir_to_node_id(i.hir_id)).unwrap()
self.definitions
.opt_local_def_id(self.definitions.hir_id_to_node_id(i.hir_id))
.unwrap()
);
self.with_dep_node_owner(i.hir_id.owner, i, |this, hash| {
this.insert_with_hash(i.span, i.hir_id, Node::Item(i), hash);
Expand Down Expand Up @@ -374,7 +376,9 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
fn visit_trait_item(&mut self, ti: &'hir TraitItem<'hir>) {
debug_assert_eq!(
ti.hir_id.owner,
self.definitions.opt_local_def_id(self.definitions.hir_to_node_id(ti.hir_id)).unwrap()
self.definitions
.opt_local_def_id(self.definitions.hir_id_to_node_id(ti.hir_id))
.unwrap()
);
self.with_dep_node_owner(ti.hir_id.owner, ti, |this, hash| {
this.insert_with_hash(ti.span, ti.hir_id, Node::TraitItem(ti), hash);
Expand All @@ -388,7 +392,9 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
fn visit_impl_item(&mut self, ii: &'hir ImplItem<'hir>) {
debug_assert_eq!(
ii.hir_id.owner,
self.definitions.opt_local_def_id(self.definitions.hir_to_node_id(ii.hir_id)).unwrap()
self.definitions
.opt_local_def_id(self.definitions.hir_id_to_node_id(ii.hir_id))
.unwrap()
);
self.with_dep_node_owner(ii.hir_id.owner, ii, |this, hash| {
this.insert_with_hash(ii.span, ii.hir_id, Node::ImplItem(ii), hash);
Expand Down
12 changes: 6 additions & 6 deletions src/librustc/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ impl<'hir> Map<'hir> {
#[inline]
pub fn local_def_id_from_node_id(&self, node: NodeId) -> DefId {
self.opt_local_def_id_from_node_id(node).unwrap_or_else(|| {
let hir_id = self.node_to_hir_id(node);
let hir_id = self.node_id_to_hir_id(node);
bug!(
"local_def_id_from_node_id: no entry for `{}`, which has a map of `{:?}`",
node,
Expand All @@ -184,7 +184,7 @@ impl<'hir> Map<'hir> {

#[inline]
pub fn opt_local_def_id(&self, hir_id: HirId) -> Option<DefId> {
let node_id = self.hir_to_node_id(hir_id);
let node_id = self.hir_id_to_node_id(hir_id);
self.opt_local_def_id_from_node_id(node_id)
}

Expand All @@ -204,13 +204,13 @@ impl<'hir> Map<'hir> {
}

#[inline]
pub fn hir_to_node_id(&self, hir_id: HirId) -> NodeId {
self.tcx.definitions.hir_to_node_id(hir_id)
pub fn hir_id_to_node_id(&self, hir_id: HirId) -> NodeId {
self.tcx.definitions.hir_id_to_node_id(hir_id)
}

#[inline]
pub fn node_to_hir_id(&self, node_id: NodeId) -> HirId {
self.tcx.definitions.node_to_hir_id(node_id)
pub fn node_id_to_hir_id(&self, node_id: NodeId) -> HirId {
self.tcx.definitions.node_id_to_hir_id(node_id)
}

#[inline]
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ich/hcx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl<'a> StableHashingContext<'a> {

#[inline]
pub fn node_to_hir_id(&self, node_id: ast::NodeId) -> hir::HirId {
self.definitions.node_to_hir_id(node_id)
self.definitions.node_id_to_hir_id(node_id)
}

#[inline]
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1126,11 +1126,11 @@ impl<'tcx> TyCtxt<'tcx> {

let mut trait_map: FxHashMap<_, FxHashMap<_, _>> = FxHashMap::default();
for (k, v) in resolutions.trait_map {
let hir_id = definitions.node_to_hir_id(k);
let hir_id = definitions.node_id_to_hir_id(k);
let map = trait_map.entry(hir_id.owner).or_default();
let v = v
.into_iter()
.map(|tc| tc.map_import_ids(|id| definitions.node_to_hir_id(id)))
.map(|tc| tc.map_import_ids(|id| definitions.node_id_to_hir_id(id)))
.collect();
map.insert(hir_id.local_id, StableVec::new(v));
}
Expand All @@ -1154,7 +1154,7 @@ impl<'tcx> TyCtxt<'tcx> {
.map(|(k, v)| {
let exports: Vec<_> = v
.into_iter()
.map(|e| e.map_id(|id| definitions.node_to_hir_id(id)))
.map(|e| e.map_id(|id| definitions.node_id_to_hir_id(id)))
.collect();
(k, exports)
})
Expand Down
63 changes: 25 additions & 38 deletions src/librustc_codegen_llvm/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ use crate::ModuleLlvm;
use log::debug;
use rustc::bug;
use rustc::ty::TyCtxt;
use rustc_codegen_ssa::back::write::{
run_assembler, BitcodeSection, CodegenContext, EmitObj, ModuleConfig,
};
use rustc_codegen_ssa::back::write::{BitcodeSection, CodegenContext, EmitObj, ModuleConfig};
use rustc_codegen_ssa::traits::*;
use rustc_codegen_ssa::{CompiledModule, ModuleCodegen, RLIB_BYTECODE_EXTENSION};
use rustc_data_structures::small_c_str::SmallCStr;
Expand Down Expand Up @@ -734,53 +732,41 @@ pub(crate) unsafe fn codegen(
})?;
}

let config_emit_object_code = matches!(config.emit_obj, EmitObj::ObjectCode(_));

if config.emit_asm || (config_emit_object_code && config.no_integrated_as) {
if config.emit_asm {
let _timer = cgcx
.prof
.generic_activity_with_arg("LLVM_module_codegen_emit_asm", &module.name[..]);
let path = cgcx.output_filenames.temp_path(OutputType::Assembly, module_name);

// We can't use the same module for asm and binary output, because that triggers
// various errors like invalid IR or broken binaries, so we might have to clone the
// module to produce the asm output
let llmod = if config_emit_object_code { llvm::LLVMCloneModule(llmod) } else { llmod };
// We can't use the same module for asm and object code output,
// because that triggers various errors like invalid IR or broken
// binaries. So we must clone the module to produce the asm output
// if we are also producing object code.
let llmod = if let EmitObj::ObjectCode(_) = config.emit_obj {
llvm::LLVMCloneModule(llmod)
} else {
llmod
};
with_codegen(tm, llmod, config.no_builtins, |cpm| {
write_output_file(diag_handler, tm, cpm, llmod, &path, llvm::FileType::AssemblyFile)
})?;
}

match config.emit_obj {
EmitObj::ObjectCode(_) => {
if !config.no_integrated_as {
let _timer = cgcx.prof.generic_activity_with_arg(
"LLVM_module_codegen_emit_obj",
&module.name[..],
);
with_codegen(tm, llmod, config.no_builtins, |cpm| {
write_output_file(
diag_handler,
tm,
cpm,
llmod,
&obj_out,
llvm::FileType::ObjectFile,
)
})?;
} else {
let _timer = cgcx.prof.generic_activity_with_arg(
"LLVM_module_codegen_asm_to_obj",
&module.name[..],
);
let assembly =
cgcx.output_filenames.temp_path(OutputType::Assembly, module_name);
run_assembler(cgcx, diag_handler, &assembly, &obj_out);

if !config.emit_asm && !cgcx.save_temps {
drop(fs::remove_file(&assembly));
}
}
let _timer = cgcx
.prof
.generic_activity_with_arg("LLVM_module_codegen_emit_obj", &module.name[..]);
with_codegen(tm, llmod, config.no_builtins, |cpm| {
write_output_file(
diag_handler,
tm,
cpm,
llmod,
&obj_out,
llvm::FileType::ObjectFile,
)
})?;
}

EmitObj::Bitcode => {
Expand All @@ -802,6 +788,7 @@ pub(crate) unsafe fn codegen(

drop(handlers);
}

Ok(module.into_compiled_module(
config.emit_obj != EmitObj::None,
config.emit_bc,
Expand Down
66 changes: 1 addition & 65 deletions src/librustc_codegen_ssa/back/write.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use super::command::Command;
use super::link::{self, get_linker, remove};
use super::link::{self, remove};
use super::linker::LinkerInfo;
use super::lto::{self, SerializedModule};
use super::symbol_export::symbol_name_for_instance_in_crate;
Expand Down Expand Up @@ -116,7 +115,6 @@ pub struct ModuleConfig {
pub merge_functions: bool,
pub inline_threshold: Option<usize>,
pub new_llvm_pass_manager: Option<bool>,
pub no_integrated_as: bool,
}

impl ModuleConfig {
Expand All @@ -140,7 +138,6 @@ impl ModuleConfig {
emit_ir: false,
emit_asm: false,
emit_obj: EmitObj::None,
no_integrated_as: false,

verify_llvm_ir: false,
no_prepopulate_passes: false,
Expand Down Expand Up @@ -202,12 +199,6 @@ impl ModuleConfig {
}
}

/// Assembler name and command used by codegen when no_integrated_as is enabled
pub struct AssemblerCommand {
name: PathBuf,
cmd: Command,
}

// HACK(eddyb) work around `#[derive]` producing wrong bounds for `Clone`.
pub struct TargetMachineFactory<B: WriteBackendMethods>(
pub Arc<dyn Fn() -> Result<B::TargetMachine, String> + Send + Sync>,
Expand Down Expand Up @@ -260,8 +251,6 @@ pub struct CodegenContext<B: WriteBackendMethods> {
pub cgu_reuse_tracker: CguReuseTracker,
// Channel back to the main control thread to send messages to
pub coordinator_send: Sender<Box<dyn Any + Send>>,
// The assembler command if no_integrated_as option is enabled, None otherwise
pub assembler_cmd: Option<Arc<AssemblerCommand>>,
}

impl<B: WriteBackendMethods> CodegenContext<B> {
Expand Down Expand Up @@ -415,9 +404,6 @@ pub fn start_async_codegen<B: ExtraBackendMethods>(

modules_config.emit_pre_lto_bc = need_pre_lto_bitcode_for_incr_comp(sess);

modules_config.no_integrated_as =
tcx.sess.opts.cg.no_integrated_as || tcx.sess.target.target.options.no_integrated_as;

for output_type in sess.opts.output_types.keys() {
match *output_type {
OutputType::Bitcode => {
Expand Down Expand Up @@ -1030,17 +1016,6 @@ fn start_executing_work<B: ExtraBackendMethods>(
each_linked_rlib_for_lto.push((cnum, path.to_path_buf()));
}));

let assembler_cmd = if modules_config.no_integrated_as {
// HACK: currently we use linker (gcc) as our assembler
let (linker, flavor) = link::linker_and_flavor(sess);

let (name, mut cmd) = get_linker(sess, &linker, flavor);
cmd.args(&sess.target.target.options.asm_args);
Some(Arc::new(AssemblerCommand { name, cmd }))
} else {
None
};

let ol = if tcx.sess.opts.debugging_opts.no_codegen
|| !tcx.sess.opts.output_types.should_codegen()
{
Expand Down Expand Up @@ -1076,7 +1051,6 @@ fn start_executing_work<B: ExtraBackendMethods>(
target_pointer_width: tcx.sess.target.target.target_pointer_width.clone(),
target_arch: tcx.sess.target.target.arch.clone(),
debuginfo: tcx.sess.opts.debuginfo,
assembler_cmd,
};

// This is the "main loop" of parallel work happening for parallel codegen.
Expand Down Expand Up @@ -1610,44 +1584,6 @@ fn spawn_work<B: ExtraBackendMethods>(cgcx: CodegenContext<B>, work: WorkItem<B>
});
}

pub fn run_assembler<B: ExtraBackendMethods>(
cgcx: &CodegenContext<B>,
handler: &Handler,
assembly: &Path,
object: &Path,
) {
let assembler = cgcx.assembler_cmd.as_ref().expect("cgcx.assembler_cmd is missing?");

let pname = &assembler.name;
let mut cmd = assembler.cmd.clone();
cmd.arg("-c").arg("-o").arg(object).arg(assembly);
debug!("{:?}", cmd);

match cmd.output() {
Ok(prog) => {
if !prog.status.success() {
let mut note = prog.stderr.clone();
note.extend_from_slice(&prog.stdout);

handler
.struct_err(&format!(
"linking with `{}` failed: {}",
pname.display(),
prog.status
))
.note(&format!("{:?}", &cmd))
.note(str::from_utf8(&note[..]).unwrap())
.emit();
handler.abort_if_errors();
}
}
Err(e) => {
handler.err(&format!("could not exec the linker `{}`: {}", pname.display(), e));
handler.abort_if_errors();
}
}
}

enum SharedEmitterMessage {
Diagnostic(Diagnostic),
InlineAsmError(u32, String),
Expand Down
19 changes: 18 additions & 1 deletion src/librustc_expand/mbe/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ fn suggest_slice_pat(e: &mut DiagnosticBuilder<'_>, site_span: Span, parser: &Pa
fn emit_frag_parse_err(
mut e: DiagnosticBuilder<'_>,
parser: &Parser<'_>,
orig_parser: &mut Parser<'_>,
site_span: Span,
macro_ident: ast::Ident,
arm_span: Span,
Expand Down Expand Up @@ -118,6 +119,21 @@ fn emit_frag_parse_err(
AstFragmentKind::Pat if macro_ident.name == sym::vec => {
suggest_slice_pat(&mut e, site_span, parser);
}
// Try a statement if an expression is wanted but failed and suggest adding `;` to call.
AstFragmentKind::Expr => match parse_ast_fragment(orig_parser, AstFragmentKind::Stmts) {
Err(mut err) => err.cancel(),
Ok(_) => {
e.note(
"the macro call doesn't expand to an expression, but it can expand to a statement",
);
e.span_suggestion_verbose(
site_span.shrink_to_hi(),
"add `;` to interpret the expansion as a statement",
";".to_string(),
Applicability::MaybeIncorrect,
);
}
},
_ => annotate_err_with_kind(&mut e, kind, site_span),
};
e.emit();
Expand All @@ -126,10 +142,11 @@ fn emit_frag_parse_err(
impl<'a> ParserAnyMacro<'a> {
crate fn make(mut self: Box<ParserAnyMacro<'a>>, kind: AstFragmentKind) -> AstFragment {
let ParserAnyMacro { site_span, macro_ident, ref mut parser, arm_span } = *self;
let snapshot = &mut parser.clone();
let fragment = match parse_ast_fragment(parser, kind) {
Ok(f) => f,
Err(err) => {
emit_frag_parse_err(err, parser, site_span, macro_ident, arm_span, kind);
emit_frag_parse_err(err, parser, snapshot, site_span, macro_ident, arm_span, kind);
return kind.dummy(site_span);
}
};
Expand Down
6 changes: 2 additions & 4 deletions src/librustc_hir/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,15 +353,13 @@ impl Definitions {
}
}

// FIXME(eddyb) rename to `hir_id_to_node_id`.
#[inline]
pub fn hir_to_node_id(&self, hir_id: hir::HirId) -> ast::NodeId {
pub fn hir_id_to_node_id(&self, hir_id: hir::HirId) -> ast::NodeId {
self.hir_id_to_node_id[&hir_id]
}

// FIXME(eddyb) rename to `node_id_to_hir_id`.
#[inline]
pub fn node_to_hir_id(&self, node_id: ast::NodeId) -> hir::HirId {
pub fn node_id_to_hir_id(&self, node_id: ast::NodeId) -> hir::HirId {
self.node_id_to_hir_id[node_id]
}

Expand Down
Loading