Skip to content

Commit 87b88a1

Browse files
committed
review comment
1 parent bb0ff3e commit 87b88a1

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

compiler/rustc_passes/src/check_attr.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -2538,7 +2538,17 @@ fn check_invalid_crate_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) {
25382538
});
25392539
tcx.sess.emit_err(errors::InvalidAttrAtCrateLevel {
25402540
span: attr.span,
2541-
snippet: tcx.sess.source_map().span_to_snippet(attr.span).ok(),
2541+
sugg_span: tcx
2542+
.sess
2543+
.source_map()
2544+
.span_to_snippet(attr.span)
2545+
.ok()
2546+
.filter(|src| src.starts_with("#!["))
2547+
.map(|_| {
2548+
attr.span
2549+
.with_lo(attr.span.lo() + BytePos(1))
2550+
.with_hi(attr.span.lo() + BytePos(2))
2551+
}),
25422552
name: *attr_to_check,
25432553
item,
25442554
});

compiler/rustc_passes/src/errors.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_errors::{
1212
use rustc_hir::{self as hir, ExprKind, Target};
1313
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
1414
use rustc_middle::ty::{MainDefinition, Ty};
15-
use rustc_span::{BytePos, Span, Symbol, DUMMY_SP};
15+
use rustc_span::{Span, Symbol, DUMMY_SP};
1616

1717
use crate::check_attr::ProcMacroKind;
1818
use crate::lang_items::Duplicate;
@@ -849,7 +849,7 @@ pub struct UnknownLangItem {
849849

850850
pub struct InvalidAttrAtCrateLevel {
851851
pub span: Span,
852-
pub snippet: Option<String>,
852+
pub sugg_span: Option<Span>,
853853
pub name: Symbol,
854854
pub item: Option<ItemFollowingInnerAttr>,
855855
}
@@ -871,9 +871,9 @@ impl IntoDiagnostic<'_> for InvalidAttrAtCrateLevel {
871871
diag.set_arg("name", self.name);
872872
// Only emit an error with a suggestion if we can create a string out
873873
// of the attribute span
874-
if let Some(src) = self.snippet && src.starts_with("#![") {
874+
if let Some(span) = self.sugg_span {
875875
diag.span_suggestion_verbose(
876-
self.span.with_lo(self.span.lo() + BytePos(1)).with_hi(self.span.lo() + BytePos(2)),
876+
span,
877877
fluent::passes_suggestion,
878878
String::new(),
879879
rustc_errors::Applicability::MachineApplicable,

0 commit comments

Comments
 (0)