Skip to content
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

Rollup of 7 pull requests #101734

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions compiler/rustc_error_messages/locales/en-US/session.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,13 @@ session_target_invalid_bits_size = {$err}
session_target_stack_protector_not_supported = `-Z stack-protector={$stack_protector}` is not supported for target {$target_triple} and will be ignored

session_split_debuginfo_unstable_platform = `-Csplit-debuginfo={$debuginfo}` is unstable on this platform

session_file_is_not_writeable = output file {$file} is not writeable -- check its permissions

session_crate_name_does_not_match = `--crate-name` and `#[crate_name]` are required to match, but `{$s}` != `{$name}`

session_crate_name_invalid = crate names cannot start with a `-`, but `{$s}` has a leading hyphen

session_crate_name_empty = crate name must not be empty

session_invalid_character_in_create_name = invalid character `{$character}` in crate name: `{$crate_name}`
2 changes: 1 addition & 1 deletion compiler/rustc_errors/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ rustc_serialize = { path = "../rustc_serialize" }
rustc_span = { path = "../rustc_span" }
rustc_macros = { path = "../rustc_macros" }
rustc_data_structures = { path = "../rustc_data_structures" }
rustc_target = { path = "../rustc_target" }
rustc_hir = { path = "../rustc_hir" }
rustc_lint_defs = { path = "../rustc_lint_defs" }
rustc_target = { path = "../rustc_target" }
unicode-width = "0.1.4"
atty = "0.2"
termcolor = "1.0"
Expand Down
30 changes: 20 additions & 10 deletions compiler/rustc_errors/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,10 @@ pub trait Emitter: Translate {
SuggestionStyle::ShowAlways,
].contains(&sugg.style)
{
let substitution = &sugg.substitutions[0].parts[0].snippet.trim();
// Don't trim the substitution if it's only whitespace changes
let substitution = &sugg.substitutions[0].parts[0].snippet;
let substitution =
if substitution.trim().is_empty() { substitution } else { substitution.trim() };
let msg = if substitution.is_empty() || sugg.style.hide_inline() {
// This substitution is only removal OR we explicitly don't want to show the
// code inline (`hide_inline`). Therefore, we don't show the substitution.
Expand Down Expand Up @@ -1704,7 +1707,7 @@ impl EmitterWriter {
{
notice_capitalization |= only_capitalization;

let has_deletion = parts.iter().any(|p| p.is_deletion());
let has_deletion = parts.iter().any(|p| p.is_deletion(sm));
let is_multiline = complete.lines().count() > 1;

if let Some(span) = span.primary_span() {
Expand Down Expand Up @@ -1880,16 +1883,23 @@ impl EmitterWriter {
let span_start_pos = sm.lookup_char_pos(part.span.lo()).col_display;
let span_end_pos = sm.lookup_char_pos(part.span.hi()).col_display;

// If this addition is _only_ whitespace, then don't trim it,
// or else we're just not rendering anything.
let is_whitespace_addition = part.snippet.trim().is_empty();

// Do not underline the leading...
let start = part.snippet.len().saturating_sub(part.snippet.trim_start().len());
let start = if is_whitespace_addition {
0
} else {
part.snippet.len().saturating_sub(part.snippet.trim_start().len())
};
// ...or trailing spaces. Account for substitutions containing unicode
// characters.
let sub_len: usize = part
.snippet
.trim()
.chars()
.map(|ch| unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1))
.sum();
let sub_len: usize =
if is_whitespace_addition { &part.snippet } else { part.snippet.trim() }
.chars()
.map(|ch| unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1))
.sum();

let offset: isize = offsets
.iter()
Expand Down Expand Up @@ -2130,7 +2140,7 @@ impl EmitterWriter {
}
}

#[derive(Clone, Copy)]
#[derive(Clone, Copy, Debug)]
enum DisplaySuggestion {
Underline,
Diff,
Expand Down
19 changes: 9 additions & 10 deletions compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,21 +150,20 @@ pub struct SubstitutionHighlight {

impl SubstitutionPart {
pub fn is_addition(&self, sm: &SourceMap) -> bool {
!self.snippet.is_empty()
&& sm
.span_to_snippet(self.span)
.map_or(self.span.is_empty(), |snippet| snippet.trim().is_empty())
!self.snippet.is_empty() && !self.replaces_meaningful_content(sm)
}

pub fn is_deletion(&self) -> bool {
self.snippet.trim().is_empty()
pub fn is_deletion(&self, sm: &SourceMap) -> bool {
self.snippet.trim().is_empty() && self.replaces_meaningful_content(sm)
}

pub fn is_replacement(&self, sm: &SourceMap) -> bool {
!self.snippet.is_empty()
&& sm
.span_to_snippet(self.span)
.map_or(!self.span.is_empty(), |snippet| !snippet.trim().is_empty())
!self.snippet.is_empty() && self.replaces_meaningful_content(sm)
}

fn replaces_meaningful_content(&self, sm: &SourceMap) -> bool {
sm.span_to_snippet(self.span)
.map_or(!self.span.is_empty(), |snippet| !snippet.trim().is_empty())
}
}

Expand Down
51 changes: 50 additions & 1 deletion compiler/rustc_session/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::num::NonZeroU32;

use crate::cgu_reuse_tracker::CguReuse;
use crate::{self as rustc_session, SessionDiagnostic};
use rustc_errors::{fluent, DiagnosticBuilder, Handler, MultiSpan};
use rustc_errors::{fluent, DiagnosticBuilder, ErrorGuaranteed, Handler, MultiSpan};
use rustc_macros::SessionDiagnostic;
use rustc_span::{Span, Symbol};
use rustc_target::abi::TargetDataLayoutErrors;
Expand Down Expand Up @@ -170,3 +170,52 @@ pub struct StackProtectorNotSupportedForTarget<'a> {
pub struct SplitDebugInfoUnstablePlatform {
pub debuginfo: SplitDebuginfo,
}

#[derive(SessionDiagnostic)]
#[diag(session::file_is_not_writeable)]
pub struct FileIsNotWriteable<'a> {
pub file: &'a std::path::Path,
}

#[derive(SessionDiagnostic)]
#[diag(session::crate_name_does_not_match)]
pub struct CrateNameDoesNotMatch<'a> {
#[primary_span]
pub span: Span,
pub s: &'a str,
pub name: Symbol,
}

#[derive(SessionDiagnostic)]
#[diag(session::crate_name_invalid)]
pub struct CrateNameInvalid<'a> {
pub s: &'a str,
}

#[derive(SessionDiagnostic)]
#[diag(session::crate_name_empty)]
pub struct CrateNameEmpty {
#[primary_span]
pub span: Option<Span>,
}

pub struct InvalidCharacterInCrateName<'a> {
pub span: Option<Span>,
pub character: char,
pub crate_name: &'a str,
}

impl crate::SessionDiagnostic<'_> for InvalidCharacterInCrateName<'_> {
fn into_diagnostic(
self,
sess: &Handler,
) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> {
let mut diag = sess.struct_err(fluent::session::invalid_character_in_create_name);
if let Some(sp) = self.span {
diag.set_span(sp);
}
diag.set_arg("character", self.character);
diag.set_arg("crate_name", self.crate_name);
diag
}
}
2 changes: 2 additions & 0 deletions compiler/rustc_session/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#![feature(map_many_mut)]
#![recursion_limit = "256"]
#![allow(rustc::potential_query_instability)]
#![deny(rustc::untranslatable_diagnostic)]
#![deny(rustc::diagnostic_outside_of_impl)]

#[macro_use]
extern crate rustc_macros;
Expand Down
35 changes: 11 additions & 24 deletions compiler/rustc_session/src/output.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
//! Related to out filenames of compilation (e.g. save analysis, binaries).
use crate::config::{CrateType, Input, OutputFilenames, OutputType};
use crate::errors::{
CrateNameDoesNotMatch, CrateNameEmpty, CrateNameInvalid, FileIsNotWriteable,
InvalidCharacterInCrateName,
};
use crate::Session;
use rustc_ast as ast;
use rustc_span::symbol::sym;
Expand Down Expand Up @@ -30,11 +34,7 @@ pub fn out_filename(
/// read-only file. We should be consistent.
pub fn check_file_is_writeable(file: &Path, sess: &Session) {
if !is_writeable(file) {
sess.fatal(&format!(
"output file {} is not writeable -- check its \
permissions",
file.display()
));
sess.emit_fatal(FileIsNotWriteable { file });
}
}

Expand All @@ -61,11 +61,7 @@ pub fn find_crate_name(sess: &Session, attrs: &[ast::Attribute], input: &Input)
if let Some(ref s) = sess.opts.crate_name {
if let Some((attr, name)) = attr_crate_name {
if name.as_str() != s {
let msg = format!(
"`--crate-name` and `#[crate_name]` are \
required to match, but `{s}` != `{name}`"
);
sess.span_err(attr.span, &msg);
sess.emit_err(CrateNameDoesNotMatch { span: attr.span, s, name });
}
}
return validate(s.clone(), None);
Expand All @@ -77,11 +73,7 @@ pub fn find_crate_name(sess: &Session, attrs: &[ast::Attribute], input: &Input)
if let Input::File(ref path) = *input {
if let Some(s) = path.file_stem().and_then(|s| s.to_str()) {
if s.starts_with('-') {
let msg = format!(
"crate names cannot start with a `-`, but \
`{s}` has a leading hyphen"
);
sess.err(&msg);
sess.emit_err(CrateNameInvalid { s });
} else {
return validate(s.replace('-', "_"), None);
}
Expand All @@ -94,15 +86,9 @@ pub fn find_crate_name(sess: &Session, attrs: &[ast::Attribute], input: &Input)
pub fn validate_crate_name(sess: &Session, s: &str, sp: Option<Span>) {
let mut err_count = 0;
{
let mut say = |s: &str| {
match sp {
Some(sp) => sess.span_err(sp, s),
None => sess.err(s),
};
err_count += 1;
};
if s.is_empty() {
say("crate name must not be empty");
err_count += 1;
sess.emit_err(CrateNameEmpty { span: sp });
}
for c in s.chars() {
if c.is_alphanumeric() {
Expand All @@ -111,7 +97,8 @@ pub fn validate_crate_name(sess: &Session, s: &str, sp: Option<Span>) {
if c == '_' {
continue;
}
say(&format!("invalid character `{c}` in crate name: `{s}`"));
err_count += 1;
sess.emit_err(InvalidCharacterInCrateName { span: sp, character: c, crate_name: s });
}
}

Expand Down
Loading