Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 9fb32dc

Browse files
committed
Auto merge of rust-lang#99151 - Dylan-DPC:rollup-40aqkxy, r=Dylan-DPC
Rollup of 5 pull requests Successful merges: - rust-lang#98882 (explain doc comments in macros a bit) - rust-lang#98907 (Deny float const params even when `adt_const_params` is enabled) - rust-lang#99091 (Do not mention private types from other crates as impl candidates) - rust-lang#99140 (Implement `SourceMap::is_span_accessible`) - rust-lang#99147 (Mention similarly named associated type even if it's not clearly in supertrait) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 7d1f57a + 21d6b1f commit 9fb32dc

File tree

31 files changed

+348
-96
lines changed

31 files changed

+348
-96
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
309309
));
310310

311311
// Check first whether the source is accessible (issue #87060)
312-
if self.infcx.tcx.sess.source_map().span_to_snippet(deref_target).is_ok() {
312+
if self.infcx.tcx.sess.source_map().is_span_accessible(deref_target) {
313313
err.span_note(deref_target, "deref defined here");
314314
}
315315
}

compiler/rustc_borrowck/src/diagnostics/mod.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -975,14 +975,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
975975
if self.fn_self_span_reported.insert(fn_span) {
976976
err.span_note(
977977
// Check whether the source is accessible
978-
if self
979-
.infcx
980-
.tcx
981-
.sess
982-
.source_map()
983-
.span_to_snippet(self_arg.span)
984-
.is_ok()
985-
{
978+
if self.infcx.tcx.sess.source_map().is_span_accessible(self_arg.span) {
986979
self_arg.span
987980
} else {
988981
fn_call_span

compiler/rustc_const_eval/src/transform/check_consts/ops.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
299299
err.note(&format!("attempting to deref into `{}`", deref_target_ty));
300300

301301
// Check first whether the source is accessible (issue #87060)
302-
if tcx.sess.source_map().span_to_snippet(deref_target).is_ok() {
302+
if tcx.sess.source_map().is_span_accessible(deref_target) {
303303
err.span_note(deref_target, "deref defined here");
304304
}
305305

compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ impl Qualif for CustomEq {
226226
// because that component may be part of an enum variant (e.g.,
227227
// `Option::<NonStructuralMatchTy>::Some`), in which case some values of this type may be
228228
// structural-match (`Option::None`).
229-
traits::search_for_structural_match_violation(cx.body.span, cx.tcx, ty).is_some()
229+
traits::search_for_structural_match_violation(cx.body.span, cx.tcx, ty, true).is_some()
230230
}
231231

232232
fn in_adt_inherently<'tcx>(
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
expand-explain-doc-comment-outer =
2+
outer doc comments expand to `#[doc = "..."]`, which is what this macro attempted to match
3+
4+
expand-explain-doc-comment-inner =
5+
inner doc comments expand to `#![doc = "..."]`, which is what this macro attempted to match

compiler/rustc_error_messages/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ pub use unic_langid::{langid, LanguageIdentifier};
3333
fluent_messages! {
3434
borrowck => "../locales/en-US/borrowck.ftl",
3535
builtin_macros => "../locales/en-US/builtin_macros.ftl",
36+
const_eval => "../locales/en-US/const_eval.ftl",
37+
expand => "../locales/en-US/expand.ftl",
3638
lint => "../locales/en-US/lint.ftl",
3739
parser => "../locales/en-US/parser.ftl",
3840
privacy => "../locales/en-US/privacy.ftl",
3941
typeck => "../locales/en-US/typeck.ftl",
40-
const_eval => "../locales/en-US/const_eval.ftl",
4142
}
4243

4344
pub use fluent_generated::{self as fluent, DEFAULT_LOCALE_RESOURCES};

compiler/rustc_errors/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1558,7 +1558,7 @@ pub fn add_elided_lifetime_in_path_suggestion(
15581558
insertion_span: Span,
15591559
) {
15601560
diag.span_label(path_span, format!("expected lifetime parameter{}", pluralize!(n)));
1561-
if source_map.span_to_snippet(insertion_span).is_err() {
1561+
if !source_map.is_span_accessible(insertion_span) {
15621562
// Do not try to suggest anything if generated by a proc-macro.
15631563
return;
15641564
}

compiler/rustc_expand/src/mbe/macro_rules.rs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_ast::{NodeId, DUMMY_NODE_ID};
1414
use rustc_ast_pretty::pprust;
1515
use rustc_attr::{self as attr, TransparencyError};
1616
use rustc_data_structures::fx::FxHashMap;
17-
use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder};
17+
use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed};
1818
use rustc_feature::Features;
1919
use rustc_lint_defs::builtin::{
2020
RUST_2021_INCOMPATIBLE_OR_PATTERNS, SEMICOLON_IN_EXPRESSIONS_FROM_MACROS,
@@ -25,6 +25,7 @@ use rustc_session::parse::ParseSess;
2525
use rustc_session::Session;
2626
use rustc_span::edition::Edition;
2727
use rustc_span::hygiene::Transparency;
28+
use rustc_span::source_map::SourceMap;
2829
use rustc_span::symbol::{kw, sym, Ident, MacroRulesNormalizedIdent};
2930
use rustc_span::Span;
3031

@@ -345,7 +346,7 @@ fn expand_macro<'cx>(
345346
if !def_span.is_dummy() && !cx.source_map().is_imported(def_span) {
346347
err.span_label(cx.source_map().guess_head_span(def_span), "when calling this macro");
347348
}
348-
349+
annotate_doc_comment(&mut err, sess.source_map(), span);
349350
// Check whether there's a missing comma in this macro call, like `println!("{}" a);`
350351
if let Some((arg, comma_span)) = arg.add_comma() {
351352
for lhs in lhses {
@@ -453,7 +454,10 @@ pub fn compile_declarative_macro(
453454
Failure(token, msg) => {
454455
let s = parse_failure_msg(&token);
455456
let sp = token.span.substitute_dummy(def.span);
456-
sess.parse_sess.span_diagnostic.struct_span_err(sp, &s).span_label(sp, msg).emit();
457+
let mut err = sess.parse_sess.span_diagnostic.struct_span_err(sp, &s);
458+
err.span_label(sp, msg);
459+
annotate_doc_comment(&mut err, sess.source_map(), sp);
460+
err.emit();
457461
return dummy_syn_ext();
458462
}
459463
Error(sp, msg) => {
@@ -590,6 +594,34 @@ pub fn compile_declarative_macro(
590594
(mk_syn_ext(expander), rule_spans)
591595
}
592596

597+
#[derive(SessionSubdiagnostic)]
598+
enum ExplainDocComment {
599+
#[label(expand::explain_doc_comment_inner)]
600+
Inner {
601+
#[primary_span]
602+
span: Span,
603+
},
604+
#[label(expand::explain_doc_comment_outer)]
605+
Outer {
606+
#[primary_span]
607+
span: Span,
608+
},
609+
}
610+
611+
fn annotate_doc_comment(
612+
err: &mut DiagnosticBuilder<'_, ErrorGuaranteed>,
613+
sm: &SourceMap,
614+
span: Span,
615+
) {
616+
if let Ok(src) = sm.span_to_snippet(span) {
617+
if src.starts_with("///") || src.starts_with("/**") {
618+
err.subdiagnostic(ExplainDocComment::Outer { span });
619+
} else if src.starts_with("//!") || src.starts_with("/*!") {
620+
err.subdiagnostic(ExplainDocComment::Inner { span });
621+
}
622+
}
623+
}
624+
593625
fn check_lhs_nt_follows(sess: &ParseSess, def: &ast::Item, lhs: &mbe::TokenTree) -> bool {
594626
// lhs is going to be like TokenTree::Delimited(...), where the
595627
// entire lhs is those tts. Or, it can be a "bare sequence", not wrapped in parens.

compiler/rustc_mir_build/src/thir/pattern/check_match.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ impl<'p, 'tcx> MatchVisitor<'_, 'p, 'tcx> {
432432
"`let` bindings require an \"irrefutable pattern\", like a `struct` or \
433433
an `enum` with only one variant",
434434
);
435-
if self.tcx.sess.source_map().span_to_snippet(span).is_ok() {
435+
if self.tcx.sess.source_map().is_span_accessible(span) {
436436
let semi_span = span.shrink_to_hi().with_lo(span.hi() - BytePos(1));
437437
let start_span = span.shrink_to_lo();
438438
let end_span = semi_span.shrink_to_lo();

compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -120,32 +120,37 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
120120
}
121121

122122
fn search_for_structural_match_violation(&self, ty: Ty<'tcx>) -> Option<String> {
123-
traits::search_for_structural_match_violation(self.span, self.tcx(), ty).map(|non_sm_ty| {
124-
with_no_trimmed_paths!(match non_sm_ty.kind {
125-
traits::NonStructuralMatchTyKind::Adt(adt) => self.adt_derive_msg(adt),
126-
traits::NonStructuralMatchTyKind::Dynamic => {
127-
"trait objects cannot be used in patterns".to_string()
128-
}
129-
traits::NonStructuralMatchTyKind::Opaque => {
130-
"opaque types cannot be used in patterns".to_string()
131-
}
132-
traits::NonStructuralMatchTyKind::Closure => {
133-
"closures cannot be used in patterns".to_string()
134-
}
135-
traits::NonStructuralMatchTyKind::Generator => {
136-
"generators cannot be used in patterns".to_string()
137-
}
138-
traits::NonStructuralMatchTyKind::Param => {
139-
bug!("use of a constant whose type is a parameter inside a pattern")
140-
}
141-
traits::NonStructuralMatchTyKind::Projection => {
142-
bug!("use of a constant whose type is a projection inside a pattern")
143-
}
144-
traits::NonStructuralMatchTyKind::Foreign => {
145-
bug!("use of a value of a foreign type inside a pattern")
146-
}
147-
})
148-
})
123+
traits::search_for_structural_match_violation(self.span, self.tcx(), ty, true).map(
124+
|non_sm_ty| {
125+
with_no_trimmed_paths!(match non_sm_ty.kind {
126+
traits::NonStructuralMatchTyKind::Adt(adt) => self.adt_derive_msg(adt),
127+
traits::NonStructuralMatchTyKind::Dynamic => {
128+
"trait objects cannot be used in patterns".to_string()
129+
}
130+
traits::NonStructuralMatchTyKind::Opaque => {
131+
"opaque types cannot be used in patterns".to_string()
132+
}
133+
traits::NonStructuralMatchTyKind::Closure => {
134+
"closures cannot be used in patterns".to_string()
135+
}
136+
traits::NonStructuralMatchTyKind::Generator => {
137+
"generators cannot be used in patterns".to_string()
138+
}
139+
traits::NonStructuralMatchTyKind::Float => {
140+
"floating-point numbers cannot be used in patterns".to_string()
141+
}
142+
traits::NonStructuralMatchTyKind::Param => {
143+
bug!("use of a constant whose type is a parameter inside a pattern")
144+
}
145+
traits::NonStructuralMatchTyKind::Projection => {
146+
bug!("use of a constant whose type is a projection inside a pattern")
147+
}
148+
traits::NonStructuralMatchTyKind::Foreign => {
149+
bug!("use of a value of a foreign type inside a pattern")
150+
}
151+
})
152+
},
153+
)
149154
}
150155

151156
fn type_marked_structural(&self, ty: Ty<'tcx>) -> bool {

0 commit comments

Comments
 (0)