Skip to content

Commit f8f5968

Browse files
committed
Auto merge of rust-lang#84564 - Dylan-DPC:rollup-wxa2yr0, r=Dylan-DPC
Rollup of 8 pull requests Successful merges: - rust-lang#84235 (refactor StyledBuffer) - rust-lang#84450 (Give a better error when `std` or `core` are missing) - rust-lang#84486 (Handle pretty printing of `else if let` clauses without ICEing) - rust-lang#84499 (Tweak trait not `use`d suggestion) - rust-lang#84516 (Add suggestion to "use break" when attempting to implicit-break a loop) - rust-lang#84520 (Improve diagnostics for function passed when a type was expected.) - rust-lang#84541 (Inline most raw socket, fd and handle conversions) - rust-lang#84547 (Get rid of is_min_const_fn) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 3709ae3 + 000a630 commit f8f5968

Some content is hidden

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

50 files changed

+623
-197
lines changed

compiler/rustc_errors/src/styled_buffer.rs

+55-35
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,52 @@
11
// Code for creating styled buffers
22

33
use crate::snippet::{Style, StyledString};
4-
use std::iter;
54

65
#[derive(Debug)]
76
pub struct StyledBuffer {
8-
text: Vec<Vec<char>>,
9-
styles: Vec<Vec<Style>>,
7+
lines: Vec<Vec<StyledChar>>,
8+
}
9+
10+
#[derive(Debug, Clone)]
11+
struct StyledChar {
12+
chr: char,
13+
style: Style,
14+
}
15+
16+
impl StyledChar {
17+
const SPACE: Self = StyledChar::new(' ', Style::NoStyle);
18+
19+
const fn new(chr: char, style: Style) -> Self {
20+
StyledChar { chr, style }
21+
}
1022
}
1123

1224
impl StyledBuffer {
1325
pub fn new() -> StyledBuffer {
14-
StyledBuffer { text: vec![], styles: vec![] }
26+
StyledBuffer { lines: vec![] }
1527
}
1628

29+
/// Returns content of `StyledBuffer` splitted by lines and line styles
1730
pub fn render(&self) -> Vec<Vec<StyledString>> {
1831
// Tabs are assumed to have been replaced by spaces in calling code.
19-
debug_assert!(self.text.iter().all(|r| !r.contains(&'\t')));
32+
debug_assert!(self.lines.iter().all(|r| !r.iter().any(|sc| sc.chr == '\t')));
2033

2134
let mut output: Vec<Vec<StyledString>> = vec![];
2235
let mut styled_vec: Vec<StyledString> = vec![];
2336

24-
for (row, row_style) in iter::zip(&self.text, &self.styles) {
37+
for styled_line in &self.lines {
2538
let mut current_style = Style::NoStyle;
2639
let mut current_text = String::new();
2740

28-
for (&c, &s) in iter::zip(row, row_style) {
29-
if s != current_style {
41+
for sc in styled_line {
42+
if sc.style != current_style {
3043
if !current_text.is_empty() {
3144
styled_vec.push(StyledString { text: current_text, style: current_style });
3245
}
33-
current_style = s;
46+
current_style = sc.style;
3447
current_text = String::new();
3548
}
36-
current_text.push(c);
49+
current_text.push(sc.chr);
3750
}
3851
if !current_text.is_empty() {
3952
styled_vec.push(StyledString { text: current_text, style: current_style });
@@ -49,29 +62,25 @@ impl StyledBuffer {
4962
}
5063

5164
fn ensure_lines(&mut self, line: usize) {
52-
while line >= self.text.len() {
53-
self.text.push(vec![]);
54-
self.styles.push(vec![]);
65+
if line >= self.lines.len() {
66+
self.lines.resize(line + 1, Vec::new());
5567
}
5668
}
5769

70+
/// Sets `chr` with `style` for given `line`, `col`.
71+
/// If `line` does not exist in our buffer, adds empty lines up to the given
72+
/// and fills the last line with unstyled whitespace.
5873
pub fn putc(&mut self, line: usize, col: usize, chr: char, style: Style) {
5974
self.ensure_lines(line);
60-
if col < self.text[line].len() {
61-
self.text[line][col] = chr;
62-
self.styles[line][col] = style;
63-
} else {
64-
let mut i = self.text[line].len();
65-
while i < col {
66-
self.text[line].push(' ');
67-
self.styles[line].push(Style::NoStyle);
68-
i += 1;
69-
}
70-
self.text[line].push(chr);
71-
self.styles[line].push(style);
75+
if col >= self.lines[line].len() {
76+
self.lines[line].resize(col + 1, StyledChar::SPACE);
7277
}
78+
self.lines[line][col] = StyledChar::new(chr, style);
7379
}
7480

81+
/// Sets `string` with `style` for given `line`, starting from `col`.
82+
/// If `line` does not exist in our buffer, adds empty lines up to the given
83+
/// and fills the last line with unstyled whitespace.
7584
pub fn puts(&mut self, line: usize, col: usize, string: &str, style: Style) {
7685
let mut n = col;
7786
for c in string.chars() {
@@ -80,32 +89,40 @@ impl StyledBuffer {
8089
}
8190
}
8291

92+
/// For given `line` inserts `string` with `style` before old content of that line,
93+
/// adding lines if needed
8394
pub fn prepend(&mut self, line: usize, string: &str, style: Style) {
8495
self.ensure_lines(line);
8596
let string_len = string.chars().count();
8697

87-
// Push the old content over to make room for new content
88-
for _ in 0..string_len {
89-
self.styles[line].insert(0, Style::NoStyle);
90-
self.text[line].insert(0, ' ');
98+
if !self.lines[line].is_empty() {
99+
// Push the old content over to make room for new content
100+
for _ in 0..string_len {
101+
self.lines[line].insert(0, StyledChar::SPACE);
102+
}
91103
}
92104

93105
self.puts(line, 0, string, style);
94106
}
95107

108+
/// For given `line` inserts `string` with `style` after old content of that line,
109+
/// adding lines if needed
96110
pub fn append(&mut self, line: usize, string: &str, style: Style) {
97-
if line >= self.text.len() {
111+
if line >= self.lines.len() {
98112
self.puts(line, 0, string, style);
99113
} else {
100-
let col = self.text[line].len();
114+
let col = self.lines[line].len();
101115
self.puts(line, col, string, style);
102116
}
103117
}
104118

105119
pub fn num_lines(&self) -> usize {
106-
self.text.len()
120+
self.lines.len()
107121
}
108122

123+
/// Set `style` for `line`, `col_start..col_end` range if:
124+
/// 1. That line and column range exist in `StyledBuffer`
125+
/// 2. `overwrite` is `true` or existing style is `Style::NoStyle` or `Style::Quotation`
109126
pub fn set_style_range(
110127
&mut self,
111128
line: usize,
@@ -119,10 +136,13 @@ impl StyledBuffer {
119136
}
120137
}
121138

139+
/// Set `style` for `line`, `col` if:
140+
/// 1. That line and column exist in `StyledBuffer`
141+
/// 2. `overwrite` is `true` or existing style is `Style::NoStyle` or `Style::Quotation`
122142
pub fn set_style(&mut self, line: usize, col: usize, style: Style, overwrite: bool) {
123-
if let Some(ref mut line) = self.styles.get_mut(line) {
124-
if let Some(s) = line.get_mut(col) {
125-
if *s == Style::NoStyle || *s == Style::Quotation || overwrite {
143+
if let Some(ref mut line) = self.lines.get_mut(line) {
144+
if let Some(StyledChar { style: s, .. }) = line.get_mut(col) {
145+
if overwrite || *s == Style::NoStyle || *s == Style::Quotation {
126146
*s = style;
127147
}
128148
}

compiler/rustc_hir_pretty/src/lib.rs

+22-2
Original file line numberDiff line numberDiff line change
@@ -1095,8 +1095,8 @@ impl<'a> State<'a> {
10951095

10961096
fn print_else(&mut self, els: Option<&hir::Expr<'_>>) {
10971097
match els {
1098-
Some(_else) => {
1099-
match _else.kind {
1098+
Some(else_) => {
1099+
match else_.kind {
11001100
// "another else-if"
11011101
hir::ExprKind::If(ref i, ref then, ref e) => {
11021102
self.cbox(INDENT_UNIT - 1);
@@ -1114,6 +1114,26 @@ impl<'a> State<'a> {
11141114
self.s.word(" else ");
11151115
self.print_block(&b)
11161116
}
1117+
hir::ExprKind::Match(ref expr, arms, _) => {
1118+
// else if let desugared to match
1119+
assert!(arms.len() == 2, "if let desugars to match with two arms");
1120+
1121+
self.s.word(" else ");
1122+
self.s.word("{");
1123+
1124+
self.cbox(INDENT_UNIT);
1125+
self.ibox(INDENT_UNIT);
1126+
self.word_nbsp("match");
1127+
self.print_expr_as_cond(&expr);
1128+
self.s.space();
1129+
self.bopen();
1130+
for arm in arms {
1131+
self.print_arm(arm);
1132+
}
1133+
self.bclose(expr.span);
1134+
1135+
self.s.word("}");
1136+
}
11171137
// BLEAH, constraints would be great here
11181138
_ => {
11191139
panic!("print_if saw if with weird alternative");

compiler/rustc_metadata/src/creader.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -511,8 +511,11 @@ impl<'a> CrateLoader<'a> {
511511
if dep.is_none() {
512512
self.used_extern_options.insert(name);
513513
}
514-
self.maybe_resolve_crate(name, dep_kind, dep)
515-
.unwrap_or_else(|err| err.report(self.sess, span))
514+
self.maybe_resolve_crate(name, dep_kind, dep).unwrap_or_else(|err| {
515+
let missing_core =
516+
self.maybe_resolve_crate(sym::core, CrateDepKind::Explicit, None).is_err();
517+
err.report(&self.sess, span, missing_core)
518+
})
516519
}
517520

518521
fn maybe_resolve_crate<'b>(

compiler/rustc_metadata/src/locator.rs

+34-3
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,8 @@ pub fn find_plugin_registrar(
790790
) -> (PathBuf, CrateDisambiguator) {
791791
match find_plugin_registrar_impl(sess, metadata_loader, name) {
792792
Ok(res) => res,
793-
Err(err) => err.report(sess, span),
793+
// `core` is always available if we got as far as loading plugins.
794+
Err(err) => err.report(sess, span, false),
794795
}
795796
}
796797

@@ -883,7 +884,7 @@ crate enum CrateError {
883884
}
884885

885886
impl CrateError {
886-
crate fn report(self, sess: &Session, span: Span) -> ! {
887+
crate fn report(self, sess: &Session, span: Span, missing_core: bool) -> ! {
887888
let mut err = match self {
888889
CrateError::NonAsciiName(crate_name) => sess.struct_span_err(
889890
span,
@@ -1068,7 +1069,37 @@ impl CrateError {
10681069
if (crate_name == sym::std || crate_name == sym::core)
10691070
&& locator.triple != TargetTriple::from_triple(config::host_triple())
10701071
{
1071-
err.note(&format!("the `{}` target may not be installed", locator.triple));
1072+
if missing_core {
1073+
err.note(&format!(
1074+
"the `{}` target may not be installed",
1075+
locator.triple
1076+
));
1077+
} else {
1078+
err.note(&format!(
1079+
"the `{}` target may not support the standard library",
1080+
locator.triple
1081+
));
1082+
}
1083+
if missing_core && std::env::var("RUSTUP_HOME").is_ok() {
1084+
err.help(&format!(
1085+
"consider downloading the target with `rustup target add {}`",
1086+
locator.triple
1087+
));
1088+
}
1089+
// Suggest using #![no_std]. #[no_core] is unstable and not really supported anyway.
1090+
// NOTE: this is a dummy span if `extern crate std` was injected by the compiler.
1091+
// If it's not a dummy, that means someone added `extern crate std` explicitly and `#![no_std]` won't help.
1092+
if !missing_core && span.is_dummy() {
1093+
let current_crate =
1094+
sess.opts.crate_name.as_deref().unwrap_or("<unknown>");
1095+
err.note(&format!(
1096+
"`std` is required by `{}` because it does not declare `#![no_std]`",
1097+
current_crate
1098+
));
1099+
}
1100+
if sess.is_nightly_build() && std::env::var("CARGO").is_ok() {
1101+
err.help("consider building the standard library from source with `cargo build -Zbuild-std`");
1102+
}
10721103
} else if crate_name == sym::profiler_builtins {
10731104
err.note(&"the compiler may have been built without the profiler runtime");
10741105
}

compiler/rustc_middle/src/mir/query.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@ use super::{Field, SourceInfo};
1919

2020
#[derive(Copy, Clone, PartialEq, TyEncodable, TyDecodable, HashStable, Debug)]
2121
pub enum UnsafetyViolationKind {
22-
/// Only permitted in regular `fn`s, prohibited in `const fn`s.
22+
/// Unsafe operation outside `unsafe`.
2323
General,
24-
/// Permitted both in `const fn`s and regular `fn`s.
25-
GeneralAndConstFn,
2624
/// Unsafe operation in an `unsafe fn` but outside an `unsafe` block.
2725
/// Has to be handled as a lint for backwards compatibility.
2826
UnsafeFn,

compiler/rustc_mir/src/const_eval/fn_queries.rs

-49
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use rustc_attr as attr;
21
use rustc_hir as hir;
32
use rustc_hir::def_id::{DefId, LocalDefId};
43
use rustc_middle::hir::map::blocks::FnLikeNode;
@@ -34,54 +33,6 @@ pub fn is_unstable_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> Option<Symbol> {
3433
}
3534
}
3635

37-
/// Returns `true` if this function must conform to `min_const_fn`
38-
pub fn is_min_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
39-
// Bail out if the signature doesn't contain `const`
40-
if !tcx.is_const_fn_raw(def_id) {
41-
return false;
42-
}
43-
44-
if tcx.features().staged_api {
45-
// In order for a libstd function to be considered min_const_fn
46-
// it needs to be stable and have no `rustc_const_unstable` attribute.
47-
match tcx.lookup_const_stability(def_id) {
48-
// `rustc_const_unstable` functions don't need to conform.
49-
Some(&attr::ConstStability { ref level, .. }) if level.is_unstable() => false,
50-
None => {
51-
if let Some(stab) = tcx.lookup_stability(def_id) {
52-
if stab.level.is_stable() {
53-
tcx.sess.delay_span_bug(
54-
tcx.def_span(def_id),
55-
"stable const functions must have either `rustc_const_stable` or \
56-
`rustc_const_unstable` attribute",
57-
);
58-
// While we errored above, because we don't know if we need to conform, we
59-
// err on the "safe" side and require min_const_fn.
60-
true
61-
} else {
62-
// Unstable functions need not conform to min_const_fn.
63-
false
64-
}
65-
} else {
66-
// Internal functions are forced to conform to min_const_fn.
67-
// Annotate the internal function with a const stability attribute if
68-
// you need to use unstable features.
69-
// Note: this is an arbitrary choice that does not affect stability or const
70-
// safety or anything, it just changes whether we need to annotate some
71-
// internal functions with `rustc_const_stable` or with `rustc_const_unstable`
72-
true
73-
}
74-
}
75-
// Everything else needs to conform, because it would be callable from
76-
// other `min_const_fn` functions.
77-
_ => true,
78-
}
79-
} else {
80-
// users enabling the `const_fn` feature gate can do what they want
81-
!tcx.features().const_fn
82-
}
83-
}
84-
8536
pub fn is_parent_const_impl_raw(tcx: TyCtxt<'_>, hir_id: hir::HirId) -> bool {
8637
let parent_id = tcx.hir().get_parent_did(hir_id);
8738
if !parent_id.is_top_level_module() { is_const_impl_raw(tcx, parent_id) } else { false }

0 commit comments

Comments
 (0)