Skip to content

Commit fe7454b

Browse files
committed
Auto merge of #112805 - matthiaskrgr:rollup-r5yrefu, r=matthiaskrgr
Rollup of 7 pull requests Successful merges: - #109970 ([doc] `poll_fn`: explain how to `pin` captured state safely) - #112705 (Simplify `Span::source_callee` impl) - #112757 (Use BorrowFlag instead of explicit isize) - #112768 (Rewrite various resolve/diagnostics errors as translatable diagnostics) - #112777 (Continue folding in query normalizer on weak aliases) - #112780 (Treat TAIT equation as always ambiguous in coherence) - #112783 (Don't ICE on bound var in `reject_fn_ptr_impls`) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 4051305 + 68d3e0e commit fe7454b

File tree

20 files changed

+339
-62
lines changed

20 files changed

+339
-62
lines changed

compiler/rustc_infer/src/infer/combine.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,10 @@ impl<'tcx> InferCtxt<'tcx> {
124124
}
125125

126126
// During coherence, opaque types should be treated as *possibly*
127-
// equal to each other, even if their generic params differ, as
128-
// they could resolve to the same hidden type, even for different
129-
// generic params.
130-
(
131-
&ty::Alias(ty::Opaque, ty::AliasTy { def_id: a_def_id, .. }),
132-
&ty::Alias(ty::Opaque, ty::AliasTy { def_id: b_def_id, .. }),
133-
) if self.intercrate && a_def_id == b_def_id => {
127+
// equal to any other type (except for possibly itself). This is an
128+
// extremely heavy hammer, but can be relaxed in a fowards-compatible
129+
// way later.
130+
(&ty::Alias(ty::Opaque, _), _) | (_, &ty::Alias(ty::Opaque, _)) if self.intercrate => {
134131
relation.register_predicates([ty::Binder::dummy(ty::PredicateKind::Ambiguous)]);
135132
Ok(a)
136133
}

compiler/rustc_resolve/messages.ftl

+18
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ resolve_add_as_non_derive =
55
add as non-Derive macro
66
`#[{$macro_path}]`
77
8+
resolve_added_macro_use =
9+
have you added the `#[macro_use]` on the module/import?
10+
811
resolve_ampersand_used_without_explicit_lifetime_name =
912
`&` without an explicit lifetime name cannot be used here
1013
.note = explicit lifetime name needed here
@@ -45,9 +48,18 @@ resolve_cannot_capture_dynamic_environment_in_fn_item =
4548
can't capture dynamic environment in a fn item
4649
.help = use the `|| {"{"} ... {"}"}` closure form instead
4750
51+
resolve_cannot_find_ident_in_this_scope =
52+
cannot find {$expected} `{$ident}` in this scope
53+
4854
resolve_cannot_use_self_type_here =
4955
can't use `Self` here
5056
57+
resolve_change_import_binding =
58+
you can use `as` to change the binding name of the import
59+
60+
resolve_consider_adding_a_derive =
61+
consider adding a derive
62+
5163
resolve_const_not_member_of_trait =
5264
const `{$const_}` is not a member of trait `{$trait_}`
5365
.label = not a member of trait `{$trait_}`
@@ -74,6 +86,9 @@ resolve_expected_found =
7486
expected module, found {$res} `{$path_str}`
7587
.label = not a module
7688
89+
resolve_explicit_unsafe_traits =
90+
unsafe traits like `{$ident}` should be implemented explicitly
91+
7792
resolve_forward_declared_generic_param =
7893
generic parameters with a default cannot use forward declared identifiers
7994
.label = defaulted generic parameters cannot be forward declared
@@ -96,6 +111,9 @@ resolve_ident_bound_more_than_once_in_same_pattern =
96111
97112
resolve_imported_crate = `$crate` may not be imported
98113
114+
resolve_imports_cannot_refer_to =
115+
imports cannot refer to {$what}
116+
99117
resolve_indeterminate =
100118
cannot determine resolution for the visibility
101119

compiler/rustc_resolve/src/diagnostics.rs

+12-19
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ use rustc_span::symbol::{kw, sym, Ident, Symbol};
3030
use rustc_span::{BytePos, Span, SyntaxContext};
3131
use thin_vec::ThinVec;
3232

33+
use crate::errors::{
34+
AddedMacroUse, ChangeImportBinding, ChangeImportBindingSuggestion, ConsiderAddingADerive,
35+
ExplicitUnsafeTraits,
36+
};
3337
use crate::imports::{Import, ImportKind};
3438
use crate::late::{PatternSource, Rib};
3539
use crate::path_names_to_string;
@@ -376,16 +380,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
376380
_ => unreachable!(),
377381
}
378382

379-
let rename_msg = "you can use `as` to change the binding name of the import";
380383
if let Some(suggestion) = suggestion {
381-
err.span_suggestion(
382-
binding_span,
383-
rename_msg,
384-
suggestion,
385-
Applicability::MaybeIncorrect,
386-
);
384+
err.subdiagnostic(ChangeImportBindingSuggestion { span: binding_span, suggestion });
387385
} else {
388-
err.span_label(binding_span, rename_msg);
386+
err.subdiagnostic(ChangeImportBinding { span: binding_span });
389387
}
390388
}
391389

@@ -1382,12 +1380,11 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13821380
);
13831381

13841382
if macro_kind == MacroKind::Derive && (ident.name == sym::Send || ident.name == sym::Sync) {
1385-
let msg = format!("unsafe traits like `{}` should be implemented explicitly", ident);
1386-
err.span_note(ident.span, msg);
1383+
err.subdiagnostic(ExplicitUnsafeTraits { span: ident.span, ident });
13871384
return;
13881385
}
13891386
if self.macro_names.contains(&ident.normalize_to_macros_2_0()) {
1390-
err.help("have you added the `#[macro_use]` on the module/import?");
1387+
err.subdiagnostic(AddedMacroUse);
13911388
return;
13921389
}
13931390
if ident.name == kw::Default
@@ -1396,14 +1393,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13961393
let span = self.def_span(def_id);
13971394
let source_map = self.tcx.sess.source_map();
13981395
let head_span = source_map.guess_head_span(span);
1399-
if let Ok(head) = source_map.span_to_snippet(head_span) {
1400-
err.span_suggestion(head_span, "consider adding a derive", format!("#[derive(Default)]\n{head}"), Applicability::MaybeIncorrect);
1401-
} else {
1402-
err.span_help(
1403-
head_span,
1404-
"consider adding `#[derive(Default)]` to this enum",
1405-
);
1406-
}
1396+
err.subdiagnostic(ConsiderAddingADerive {
1397+
span: head_span.shrink_to_lo(),
1398+
suggestion: format!("#[derive(Default)]\n")
1399+
});
14071400
}
14081401
for ns in [Namespace::MacroNS, Namespace::TypeNS, Namespace::ValueNS] {
14091402
if let Ok(binding) = self.early_resolve_ident_in_lexical_scope(

compiler/rustc_resolve/src/errors.rs

+60
Original file line numberDiff line numberDiff line change
@@ -586,3 +586,63 @@ pub(crate) enum ParamKindInEnumDiscriminant {
586586
#[note(resolve_lifetime_param_in_enum_discriminant)]
587587
Lifetime,
588588
}
589+
590+
#[derive(Subdiagnostic)]
591+
#[label(resolve_change_import_binding)]
592+
pub(crate) struct ChangeImportBinding {
593+
#[primary_span]
594+
pub(crate) span: Span,
595+
}
596+
597+
#[derive(Subdiagnostic)]
598+
#[suggestion(
599+
resolve_change_import_binding,
600+
code = "{suggestion}",
601+
applicability = "maybe-incorrect"
602+
)]
603+
pub(crate) struct ChangeImportBindingSuggestion {
604+
#[primary_span]
605+
pub(crate) span: Span,
606+
pub(crate) suggestion: String,
607+
}
608+
609+
#[derive(Diagnostic)]
610+
#[diag(resolve_imports_cannot_refer_to)]
611+
pub(crate) struct ImportsCannotReferTo<'a> {
612+
#[primary_span]
613+
pub(crate) span: Span,
614+
pub(crate) what: &'a str,
615+
}
616+
617+
#[derive(Diagnostic)]
618+
#[diag(resolve_cannot_find_ident_in_this_scope)]
619+
pub(crate) struct CannotFindIdentInThisScope<'a> {
620+
#[primary_span]
621+
pub(crate) span: Span,
622+
pub(crate) expected: &'a str,
623+
pub(crate) ident: Ident,
624+
}
625+
626+
#[derive(Subdiagnostic)]
627+
#[note(resolve_explicit_unsafe_traits)]
628+
pub(crate) struct ExplicitUnsafeTraits {
629+
#[primary_span]
630+
pub(crate) span: Span,
631+
pub(crate) ident: Ident,
632+
}
633+
634+
#[derive(Subdiagnostic)]
635+
#[help(resolve_added_macro_use)]
636+
pub(crate) struct AddedMacroUse;
637+
638+
#[derive(Subdiagnostic)]
639+
#[suggestion(
640+
resolve_consider_adding_a_derive,
641+
code = "{suggestion}",
642+
applicability = "maybe-incorrect"
643+
)]
644+
pub(crate) struct ConsiderAddingADerive {
645+
#[primary_span]
646+
pub(crate) span: Span,
647+
pub(crate) suggestion: String,
648+
}

compiler/rustc_resolve/src/late.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//! If you wonder why there's no `early.rs`, that's because it's split into three files -
77
//! `build_reduced_graph.rs`, `macros.rs` and `imports.rs`.
88
9+
use crate::errors::ImportsCannotReferTo;
910
use crate::BindingKey;
1011
use crate::{path_names_to_string, rustdoc, BindingError, Finalize, LexicalScopeBinding};
1112
use crate::{Module, ModuleOrUniformRoot, NameBinding, ParentScope, PathResult};
@@ -2244,12 +2245,13 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
22442245
_ => &[TypeNS],
22452246
};
22462247
let report_error = |this: &Self, ns| {
2247-
let what = if ns == TypeNS { "type parameters" } else { "local variables" };
22482248
if this.should_report_errs() {
2249+
let what = if ns == TypeNS { "type parameters" } else { "local variables" };
22492250
this.r
22502251
.tcx
22512252
.sess
2252-
.span_err(ident.span, format!("imports cannot refer to {}", what));
2253+
.create_err(ImportsCannotReferTo { span: ident.span, what })
2254+
.emit();
22532255
}
22542256
};
22552257

compiler/rustc_resolve/src/macros.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
//! A bunch of methods and structures more or less related to resolving macros and
22
//! interface provided by `Resolver` to macro expander.
33
4-
use crate::errors::{self, AddAsNonDerive, MacroExpectedFound, RemoveSurroundingDerive};
4+
use crate::errors::{
5+
self, AddAsNonDerive, CannotFindIdentInThisScope, MacroExpectedFound, RemoveSurroundingDerive,
6+
};
57
use crate::Namespace::*;
68
use crate::{BuiltinMacroState, Determinacy};
79
use crate::{DeriveData, Finalize, ParentScope, ResolutionError, Resolver, ScopeSet};
@@ -793,8 +795,13 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
793795
}
794796
Err(..) => {
795797
let expected = kind.descr_expected();
796-
let msg = format!("cannot find {} `{}` in this scope", expected, ident);
797-
let mut err = self.tcx.sess.struct_span_err(ident.span, msg);
798+
799+
let mut err = self.tcx.sess.create_err(CannotFindIdentInThisScope {
800+
span: ident.span,
801+
expected,
802+
ident,
803+
});
804+
798805
self.unresolved_macro_suggestions(&mut err, kind, &parent_scope, ident, krate);
799806
err.emit();
800807
}

compiler/rustc_span/src/lib.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ use rustc_data_structures::sync::{Lock, Lrc};
6565

6666
use std::borrow::Cow;
6767
use std::cmp::{self, Ordering};
68-
use std::fmt;
6968
use std::hash::Hash;
7069
use std::ops::{Add, Range, Sub};
7170
use std::path::{Path, PathBuf};
7271
use std::str::FromStr;
72+
use std::{fmt, iter};
7373

7474
use md5::Digest;
7575
use md5::Md5;
@@ -733,12 +733,15 @@ impl Span {
733733
/// else returns the `ExpnData` for the macro definition
734734
/// corresponding to the source callsite.
735735
pub fn source_callee(self) -> Option<ExpnData> {
736-
fn source_callee(expn_data: ExpnData) -> ExpnData {
737-
let next_expn_data = expn_data.call_site.ctxt().outer_expn_data();
738-
if !next_expn_data.is_root() { source_callee(next_expn_data) } else { expn_data }
739-
}
740736
let expn_data = self.ctxt().outer_expn_data();
741-
if !expn_data.is_root() { Some(source_callee(expn_data)) } else { None }
737+
738+
// Create an iterator of call site expansions
739+
iter::successors(Some(expn_data), |expn_data| {
740+
Some(expn_data.call_site.ctxt().outer_expn_data())
741+
})
742+
// Find the last expansion which is not root
743+
.take_while(|expn_data| !expn_data.is_root())
744+
.last()
742745
}
743746

744747
/// Checks if a span is "internal" to a macro in which `#[unstable]`
@@ -777,7 +780,7 @@ impl Span {
777780

778781
pub fn macro_backtrace(mut self) -> impl Iterator<Item = ExpnData> {
779782
let mut prev_span = DUMMY_SP;
780-
std::iter::from_fn(move || {
783+
iter::from_fn(move || {
781784
loop {
782785
let expn_data = self.ctxt().outer_expn_data();
783786
if expn_data.is_root() {

compiler/rustc_trait_selection/src/traits/query/normalize.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,12 @@ impl<'cx, 'tcx> FallibleTypeFolder<TyCtxt<'tcx>> for QueryNormalizer<'cx, 'tcx>
322322
};
323323
// `tcx.normalize_projection_ty` may normalize to a type that still has
324324
// unevaluated consts, so keep normalizing here if that's the case.
325-
if res != ty && res.has_type_flags(ty::TypeFlags::HAS_CT_PROJECTION) {
326-
res.try_super_fold_with(self)?
325+
// Similarly, `tcx.normalize_weak_ty` will only unwrap one layer of type
326+
// and we need to continue folding it to reveal the TAIT behind it.
327+
if res != ty
328+
&& (res.has_type_flags(ty::TypeFlags::HAS_CT_PROJECTION) || kind == ty::Weak)
329+
{
330+
res.try_fold_with(self)?
327331
} else {
328332
res
329333
}

compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -417,17 +417,15 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
417417
// Fast path to avoid evaluating an obligation that trivially holds.
418418
// There may be more bounds, but these are checked by the regular path.
419419
ty::FnPtr(..) => return false,
420+
420421
// These may potentially implement `FnPtr`
421422
ty::Placeholder(..)
422423
| ty::Dynamic(_, _, _)
423424
| ty::Alias(_, _)
424425
| ty::Infer(_)
425-
| ty::Param(..) => {}
426+
| ty::Param(..)
427+
| ty::Bound(_, _) => {}
426428

427-
ty::Bound(_, _) => span_bug!(
428-
obligation.cause.span(),
429-
"cannot have escaping bound var in self type of {obligation:#?}"
430-
),
431429
// These can't possibly implement `FnPtr` as they are concrete types
432430
// and not `FnPtr`
433431
ty::Bool

library/core/src/cell.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1374,7 +1374,7 @@ impl Clone for BorrowRef<'_> {
13741374
debug_assert!(is_reading(borrow));
13751375
// Prevent the borrow counter from overflowing into
13761376
// a writing borrow.
1377-
assert!(borrow != isize::MAX);
1377+
assert!(borrow != BorrowFlag::MAX);
13781378
self.borrow.set(borrow + 1);
13791379
BorrowRef { borrow: self.borrow }
13801380
}
@@ -1756,7 +1756,7 @@ impl<'b> BorrowRefMut<'b> {
17561756
let borrow = self.borrow.get();
17571757
debug_assert!(is_writing(borrow));
17581758
// Prevent the borrow counter from underflowing.
1759-
assert!(borrow != isize::MIN);
1759+
assert!(borrow != BorrowFlag::MIN);
17601760
self.borrow.set(borrow - 1);
17611761
BorrowRefMut { borrow: self.borrow }
17621762
}

0 commit comments

Comments
 (0)