Skip to content

Commit db0597f

Browse files
committed
Auto merge of rust-lang#102926 - matthiaskrgr:rollup-oe2cdzj, r=matthiaskrgr
Rollup of 11 pull requests Successful merges: - rust-lang#100387 (Check uniqueness of impl items by trait item when applicable.) - rust-lang#101727 (Stabilize map_first_last) - rust-lang#101774 (Warn about safety of `fetch_update`) - rust-lang#102227 (fs::get_path solarish version.) - rust-lang#102445 (Add `is_empty()` method to `core::ffi::CStr`.) - rust-lang#102612 (Migrate `codegen_ssa` to diagnostics structs - [Part 1]) - rust-lang#102685 (Interpret EH actions properly) - rust-lang#102869 (Add basename and dirname aliases) - rust-lang#102889 (rustc_hir: Less error-prone methods for accessing `PartialRes` resolution) - rust-lang#102893 (Fix ICE rust-lang#102878) - rust-lang#102912 (:arrow_up: rust-analyzer) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents cde693c + 3e01d07 commit db0597f

File tree

163 files changed

+3790
-1349
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

163 files changed

+3790
-1349
lines changed

compiler/rustc_ast_lowering/src/asm.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
205205
let static_def_id = self
206206
.resolver
207207
.get_partial_res(sym.id)
208-
.filter(|res| res.unresolved_segments() == 0)
209-
.and_then(|res| {
210-
if let Res::Def(DefKind::Static(_), def_id) = res.base_res() {
211-
Some(def_id)
212-
} else {
213-
None
214-
}
208+
.and_then(|res| res.full_res())
209+
.and_then(|res| match res {
210+
Res::Def(DefKind::Static(_), def_id) => Some(def_id),
211+
_ => None,
215212
});
216213

217214
if let Some(def_id) = static_def_id {

compiler/rustc_ast_lowering/src/expr.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -1044,9 +1044,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10441044
if let ExprKind::Path(qself, path) = &expr.kind {
10451045
// Does the path resolve to something disallowed in a tuple struct/variant pattern?
10461046
if let Some(partial_res) = self.resolver.get_partial_res(expr.id) {
1047-
if partial_res.unresolved_segments() == 0
1048-
&& !partial_res.base_res().expected_in_tuple_struct_pat()
1049-
{
1047+
if let Some(res) = partial_res.full_res() && !res.expected_in_tuple_struct_pat() {
10501048
return None;
10511049
}
10521050
}
@@ -1066,9 +1064,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10661064
if let ExprKind::Path(qself, path) = &expr.kind {
10671065
// Does the path resolve to something disallowed in a unit struct/variant pattern?
10681066
if let Some(partial_res) = self.resolver.get_partial_res(expr.id) {
1069-
if partial_res.unresolved_segments() == 0
1070-
&& !partial_res.base_res().expected_in_unit_struct_pat()
1071-
{
1067+
if let Some(res) = partial_res.full_res() && !res.expected_in_unit_struct_pat() {
10721068
return None;
10731069
}
10741070
}

compiler/rustc_ast_lowering/src/item.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
947947
}
948948
AssocItemKind::MacCall(..) => unimplemented!(),
949949
},
950-
trait_item_def_id: self.resolver.get_partial_res(i.id).map(|r| r.base_res().def_id()),
950+
trait_item_def_id: self
951+
.resolver
952+
.get_partial_res(i.id)
953+
.map(|r| r.expect_full_res().def_id()),
951954
}
952955
}
953956

@@ -1349,9 +1352,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
13491352
match self
13501353
.resolver
13511354
.get_partial_res(bound_pred.bounded_ty.id)
1352-
.map(|d| (d.base_res(), d.unresolved_segments()))
1355+
.and_then(|r| r.full_res())
13531356
{
1354-
Some((Res::Def(DefKind::TyParam, def_id), 0))
1357+
Some(Res::Def(DefKind::TyParam, def_id))
13551358
if bound_pred.bound_generic_params.is_empty() =>
13561359
{
13571360
generics

compiler/rustc_ast_lowering/src/lib.rs

+8-16
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,7 @@ impl ResolverAstLoweringExt for ResolverAstLowering {
175175
return None;
176176
}
177177

178-
let partial_res = self.partial_res_map.get(&expr.id)?;
179-
if partial_res.unresolved_segments() != 0 {
180-
return None;
181-
}
182-
183-
if let Res::Def(DefKind::Fn, def_id) = partial_res.base_res() {
178+
if let Res::Def(DefKind::Fn, def_id) = self.partial_res_map.get(&expr.id)?.full_res()? {
184179
// We only support cross-crate argument rewriting. Uses
185180
// within the same crate should be updated to use the new
186181
// const generics style.
@@ -753,12 +748,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
753748
}
754749

755750
fn expect_full_res(&mut self, id: NodeId) -> Res<NodeId> {
756-
self.resolver.get_partial_res(id).map_or(Res::Err, |pr| {
757-
if pr.unresolved_segments() != 0 {
758-
panic!("path not fully resolved: {:?}", pr);
759-
}
760-
pr.base_res()
761-
})
751+
self.resolver.get_partial_res(id).map_or(Res::Err, |pr| pr.expect_full_res())
762752
}
763753

764754
fn expect_full_res_from_use(&mut self, id: NodeId) -> impl Iterator<Item = Res<NodeId>> {
@@ -1138,8 +1128,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11381128
// type and value namespaces. If we resolved the path in the value namespace, we
11391129
// transform it into a generic const argument.
11401130
TyKind::Path(ref qself, ref path) => {
1141-
if let Some(partial_res) = self.resolver.get_partial_res(ty.id) {
1142-
let res = partial_res.base_res();
1131+
if let Some(res) = self
1132+
.resolver
1133+
.get_partial_res(ty.id)
1134+
.and_then(|partial_res| partial_res.full_res())
1135+
{
11431136
if !res.matches_ns(Namespace::TypeNS) {
11441137
debug!(
11451138
"lower_generic_arg: Lowering type argument as const argument: {:?}",
@@ -1206,8 +1199,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12061199
// by `ty_path`.
12071200
if qself.is_none()
12081201
&& let Some(partial_res) = self.resolver.get_partial_res(t.id)
1209-
&& partial_res.unresolved_segments() == 0
1210-
&& let Res::Def(DefKind::Trait | DefKind::TraitAlias, _) = partial_res.base_res()
1202+
&& let Some(Res::Def(DefKind::Trait | DefKind::TraitAlias, _)) = partial_res.full_res()
12111203
{
12121204
let (bounds, lifetime_bound) = self.with_dyn_type_scope(true, |this| {
12131205
let poly_trait_ref = this.ast_arena.ptr.alloc(PolyTraitRef {

compiler/rustc_ast_lowering/src/pat.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
239239
ident: Ident,
240240
lower_sub: impl FnOnce(&mut Self) -> Option<&'hir hir::Pat<'hir>>,
241241
) -> hir::PatKind<'hir> {
242-
match self.resolver.get_partial_res(p.id).map(|d| d.base_res()) {
242+
match self.resolver.get_partial_res(p.id).map(|d| d.expect_full_res()) {
243243
// `None` can occur in body-less function signatures
244244
res @ (None | Some(Res::Local(_))) => {
245245
let canonical_id = match res {

compiler/rustc_ast_lowering/src/path.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
2929

3030
let partial_res =
3131
self.resolver.get_partial_res(id).unwrap_or_else(|| PartialRes::new(Res::Err));
32+
let base_res = partial_res.base_res();
33+
let unresolved_segments = partial_res.unresolved_segments();
3234

3335
let path_span_lo = p.span.shrink_to_lo();
34-
let proj_start = p.segments.len() - partial_res.unresolved_segments();
36+
let proj_start = p.segments.len() - unresolved_segments;
3537
let path = self.arena.alloc(hir::Path {
36-
res: self.lower_res(partial_res.base_res()),
38+
res: self.lower_res(base_res),
3739
segments: self.arena.alloc_from_iter(p.segments[..proj_start].iter().enumerate().map(
3840
|(i, segment)| {
3941
let param_mode = match (qself_position, param_mode) {
@@ -46,7 +48,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
4648
_ => param_mode,
4749
};
4850

49-
let parenthesized_generic_args = match partial_res.base_res() {
51+
let parenthesized_generic_args = match base_res {
5052
// `a::b::Trait(Args)`
5153
Res::Def(DefKind::Trait, _) if i + 1 == proj_start => {
5254
ParenthesizedGenericArgs::Ok
@@ -83,7 +85,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
8385

8486
// Simple case, either no projections, or only fully-qualified.
8587
// E.g., `std::mem::size_of` or `<I as Iterator>::Item`.
86-
if partial_res.unresolved_segments() == 0 {
88+
if unresolved_segments == 0 {
8789
return hir::QPath::Resolved(qself, path);
8890
}
8991

compiler/rustc_codegen_ssa/src/back/link.rs

+30-52
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ use super::command::Command;
3131
use super::linker::{self, Linker};
3232
use super::metadata::{create_rmeta_file, MetadataPosition};
3333
use super::rpath::{self, RPathConfig};
34-
use crate::{looks_like_rust_object_file, CodegenResults, CompiledModule, CrateInfo, NativeLib};
34+
use crate::{
35+
errors, looks_like_rust_object_file, CodegenResults, CompiledModule, CrateInfo, NativeLib,
36+
};
3537

3638
use cc::windows_registry;
3739
use regex::Regex;
@@ -93,7 +95,7 @@ pub fn link_binary<'a>(
9395
let tmpdir = TempFileBuilder::new()
9496
.prefix("rustc")
9597
.tempdir()
96-
.unwrap_or_else(|err| sess.fatal(&format!("couldn't create a temp dir: {}", err)));
98+
.unwrap_or_else(|error| sess.emit_fatal(errors::CreateTempDir { error }));
9799
let path = MaybeTempDir::new(tmpdir, sess.opts.cg.save_temps);
98100
let out_filename = out_filename(
99101
sess,
@@ -208,7 +210,7 @@ pub fn link_binary<'a>(
208210
pub fn each_linked_rlib(
209211
info: &CrateInfo,
210212
f: &mut dyn FnMut(CrateNum, &Path),
211-
) -> Result<(), String> {
213+
) -> Result<(), errors::LinkRlibError> {
212214
let crates = info.used_crates.iter();
213215
let mut fmts = None;
214216
for (ty, list) in info.dependency_formats.iter() {
@@ -224,26 +226,23 @@ pub fn each_linked_rlib(
224226
}
225227
}
226228
let Some(fmts) = fmts else {
227-
return Err("could not find formats for rlibs".to_string());
229+
return Err(errors::LinkRlibError::MissingFormat);
228230
};
229231
for &cnum in crates {
230232
match fmts.get(cnum.as_usize() - 1) {
231233
Some(&Linkage::NotLinked | &Linkage::IncludedFromDylib) => continue,
232234
Some(_) => {}
233-
None => return Err("could not find formats for rlibs".to_string()),
235+
None => return Err(errors::LinkRlibError::MissingFormat),
234236
}
235-
let name = info.crate_name[&cnum];
237+
let crate_name = info.crate_name[&cnum];
236238
let used_crate_source = &info.used_crate_source[&cnum];
237239
if let Some((path, _)) = &used_crate_source.rlib {
238240
f(cnum, &path);
239241
} else {
240242
if used_crate_source.rmeta.is_some() {
241-
return Err(format!(
242-
"could not find rlib for: `{}`, found rmeta (metadata) file",
243-
name
244-
));
243+
return Err(errors::LinkRlibError::OnlyRmetaFound { crate_name });
245244
} else {
246-
return Err(format!("could not find rlib for: `{}`", name));
245+
return Err(errors::LinkRlibError::NotFound { crate_name });
247246
}
248247
}
249248
}
@@ -340,10 +339,7 @@ fn link_rlib<'a>(
340339
// -whole-archive and it isn't clear how we can currently handle such a
341340
// situation correctly.
342341
// See https://github.com/rust-lang/rust/issues/88085#issuecomment-901050897
343-
sess.err(
344-
"the linking modifiers `+bundle` and `+whole-archive` are not compatible \
345-
with each other when generating rlibs",
346-
);
342+
sess.emit_err(errors::IncompatibleLinkingModifiers);
347343
}
348344
NativeLibKind::Static { bundle: None | Some(true), .. } => {}
349345
NativeLibKind::Static { bundle: Some(false), .. }
@@ -365,12 +361,8 @@ fn link_rlib<'a>(
365361
));
366362
continue;
367363
}
368-
ab.add_archive(&location, Box::new(|_| false)).unwrap_or_else(|e| {
369-
sess.fatal(&format!(
370-
"failed to add native library {}: {}",
371-
location.to_string_lossy(),
372-
e
373-
));
364+
ab.add_archive(&location, Box::new(|_| false)).unwrap_or_else(|error| {
365+
sess.emit_fatal(errors::AddNativeLibrary { library_path: location, error });
374366
});
375367
}
376368
}
@@ -385,8 +377,8 @@ fn link_rlib<'a>(
385377
tmpdir.as_ref(),
386378
);
387379

388-
ab.add_archive(&output_path, Box::new(|_| false)).unwrap_or_else(|e| {
389-
sess.fatal(&format!("failed to add native library {}: {}", output_path.display(), e));
380+
ab.add_archive(&output_path, Box::new(|_| false)).unwrap_or_else(|error| {
381+
sess.emit_fatal(errors::AddNativeLibrary { library_path: output_path, error });
390382
});
391383
}
392384

@@ -451,14 +443,11 @@ fn collate_raw_dylibs(
451443
// FIXME: when we add support for ordinals, figure out if we need to do anything
452444
// if we have two DllImport values with the same name but different ordinals.
453445
if import.calling_convention != old_import.calling_convention {
454-
sess.span_err(
455-
import.span,
456-
&format!(
457-
"multiple declarations of external function `{}` from \
458-
library `{}` have different calling conventions",
459-
import.name, name,
460-
),
461-
);
446+
sess.emit_err(errors::MultipleExternalFuncDecl {
447+
span: import.span,
448+
function: import.name,
449+
library_name: &name,
450+
});
462451
}
463452
}
464453
}
@@ -560,7 +549,7 @@ fn link_staticlib<'a>(
560549
all_native_libs.extend(codegen_results.crate_info.native_libraries[&cnum].iter().cloned());
561550
});
562551
if let Err(e) = res {
563-
sess.fatal(&e);
552+
sess.emit_fatal(e);
564553
}
565554

566555
ab.build(out_filename);
@@ -673,9 +662,7 @@ fn link_dwarf_object<'a>(
673662
}) {
674663
Ok(()) => {}
675664
Err(e) => {
676-
sess.struct_err("linking dwarf objects with thorin failed")
677-
.note(&format!("{:?}", e))
678-
.emit();
665+
sess.emit_err(errors::ThorinErrorWrapper(e));
679666
sess.abort_if_errors();
680667
}
681668
}
@@ -879,23 +866,14 @@ fn link_natively<'a>(
879866
let mut output = prog.stderr.clone();
880867
output.extend_from_slice(&prog.stdout);
881868
let escaped_output = escape_string(&output);
882-
let mut err = sess.struct_err(&format!(
883-
"linking with `{}` failed: {}",
884-
linker_path.display(),
885-
prog.status
886-
));
887-
err.note(&format!("{:?}", &cmd)).note(&escaped_output);
888-
if escaped_output.contains("undefined reference to") {
889-
err.help(
890-
"some `extern` functions couldn't be found; some native libraries may \
891-
need to be installed or have their path specified",
892-
);
893-
err.note("use the `-l` flag to specify native libraries to link");
894-
err.note("use the `cargo:rustc-link-lib` directive to specify the native \
895-
libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)");
896-
}
897-
err.emit();
898-
869+
// FIXME: Add UI tests for this error.
870+
let err = errors::LinkingFailed {
871+
linker_path: &linker_path,
872+
exit_status: prog.status,
873+
command: &cmd,
874+
escaped_output: &escaped_output,
875+
};
876+
sess.diagnostic().emit_err(err);
899877
// If MSVC's `link.exe` was expected but the return code
900878
// is not a Microsoft LNK error then suggest a way to fix or
901879
// install the Visual Studio build tools.

0 commit comments

Comments
 (0)