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 4 pull requests #98307

Merged
merged 10 commits into from
Jun 21, 2022
12 changes: 11 additions & 1 deletion compiler/rustc_const_eval/src/transform/check_consts/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use rustc_middle::mir;
use rustc_middle::ty::print::with_no_trimmed_paths;
use rustc_middle::ty::subst::{GenericArgKind, SubstsRef};
use rustc_middle::ty::{
suggest_constraining_type_param, Adt, Closure, FnDef, FnPtr, Param, TraitPredicate, Ty,
suggest_constraining_type_param, Adt, Closure, DefIdTree, FnDef, FnPtr, Param, TraitPredicate,
Ty,
};
use rustc_middle::ty::{Binder, BoundConstness, ImplPolarity, TraitRef};
use rustc_session::parse::feature_err;
Expand Down Expand Up @@ -300,6 +301,15 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
diag_trait(&mut err, self_ty, tcx.lang_items().deref_trait().unwrap());
err
}
_ if tcx.opt_parent(callee) == tcx.get_diagnostic_item(sym::ArgumentV1Methods) => {
struct_span_err!(
ccx.tcx.sess,
span,
E0015,
"cannot call non-const formatting macro in {}s",
ccx.const_kind(),
)
}
_ => struct_span_err!(
ccx.tcx.sess,
span,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/mir/spanview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ fn trim_span_hi(span: Span, to_pos: BytePos) -> Span {
fn fn_span<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> Span {
let fn_decl_span = tcx.def_span(def_id);
if let Some(body_span) = hir_body(tcx, def_id).map(|hir_body| hir_body.value.span) {
if fn_decl_span.ctxt() == body_span.ctxt() { fn_decl_span.to(body_span) } else { body_span }
if fn_decl_span.eq_ctxt(body_span) { fn_decl_span.to(body_span) } else { body_span }
} else {
fn_decl_span
}
Expand Down
34 changes: 23 additions & 11 deletions compiler/rustc_mir_build/src/thir/pattern/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ fn non_exhaustive_match<'p, 'tcx>(
let mut suggestion = None;
let sm = cx.tcx.sess.source_map();
match arms {
[] if sp.ctxt() == expr_span.ctxt() => {
[] if sp.eq_ctxt(expr_span) => {
// Get the span for the empty match body `{}`.
let (indentation, more) = if let Some(snippet) = sm.indentation_before(sp) {
(format!("\n{}", snippet), " ")
Expand All @@ -821,24 +821,36 @@ fn non_exhaustive_match<'p, 'tcx>(
));
}
[only] => {
let pre_indentation = if let (Some(snippet), true) = (
sm.indentation_before(only.span),
sm.is_multiline(sp.shrink_to_hi().with_hi(only.span.lo())),
) {
format!("\n{}", snippet)
let (pre_indentation, is_multiline) = if let Some(snippet) = sm.indentation_before(only.span)
&& let Ok(with_trailing) = sm.span_extend_while(only.span, |c| c.is_whitespace() || c == ',')
&& sm.is_multiline(with_trailing)
{
(format!("\n{}", snippet), true)
} else {
(" ".to_string(), false)
};
let comma = if matches!(only.body.kind, hir::ExprKind::Block(..))
&& only.span.eq_ctxt(only.body.span)
&& is_multiline
{
""
} else {
" ".to_string()
","
};
let comma = if matches!(only.body.kind, hir::ExprKind::Block(..)) { "" } else { "," };
suggestion = Some((
only.span.shrink_to_hi(),
format!("{}{}{} => todo!()", comma, pre_indentation, pattern),
));
}
[.., prev, last] if prev.span.ctxt() == last.span.ctxt() => {
[.., prev, last] if prev.span.eq_ctxt(last.span) => {
if let Ok(snippet) = sm.span_to_snippet(prev.span.between(last.span)) {
let comma =
if matches!(last.body.kind, hir::ExprKind::Block(..)) { "" } else { "," };
let comma = if matches!(last.body.kind, hir::ExprKind::Block(..))
&& last.span.eq_ctxt(last.body.span)
{
""
} else {
","
};
suggestion = Some((
last.span.shrink_to_hi(),
format!(
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_transform/src/coverage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {

let source_file = source_map.lookup_source_file(body_span.lo());
let fn_sig_span = match some_fn_sig.filter(|fn_sig| {
fn_sig.span.ctxt() == body_span.ctxt()
fn_sig.span.eq_ctxt(body_span)
&& Lrc::ptr_eq(&source_file, &source_map.lookup_source_file(fn_sig.span.lo()))
}) {
Some(fn_sig) => fn_sig.span.with_hi(body_span.lo()),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_transform/src/coverage/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ impl CoverageSpan {
.expn_span
.parent_callsite()
.unwrap_or_else(|| bug!("macro must have a parent"))
.ctxt() == body_span.ctxt()
.eq_ctxt(body_span)
{
return Some(current_macro);
}
Expand Down
8 changes: 6 additions & 2 deletions compiler/rustc_mir_transform/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ const RETURNED: usize = GeneratorSubsts::RETURNED;
/// Generator has panicked and is poisoned.
const POISONED: usize = GeneratorSubsts::POISONED;

/// Number of variants to reserve in generator state. Corresponds to
/// `UNRESUMED` (beginning of a generator) and `RETURNED`/`POISONED`
/// (end of a generator) states.
const RESERVED_VARIANTS: usize = 3;

/// A `yield` point in the generator.
struct SuspensionPoint<'tcx> {
/// State discriminant used when suspending or resuming at this point.
Expand Down Expand Up @@ -345,7 +350,7 @@ impl<'tcx> MutVisitor<'tcx> for TransformVisitor<'tcx> {
data.statements.extend(self.make_state(state_idx, v, source_info));
let state = if let Some((resume, mut resume_arg)) = resume {
// Yield
let state = 3 + self.suspension_points.len();
let state = RESERVED_VARIANTS + self.suspension_points.len();

// The resume arg target location might itself be remapped if its base local is
// live across a yield.
Expand Down Expand Up @@ -792,7 +797,6 @@ fn compute_layout<'tcx>(
// Leave empty variants for the UNRESUMED, RETURNED, and POISONED states.
// In debuginfo, these will correspond to the beginning (UNRESUMED) or end
// (RETURNED, POISONED) of the function.
const RESERVED_VARIANTS: usize = 3;
let body_span = body.source_scopes[OUTERMOST_SOURCE_SCOPE].span;
let mut variant_source_info: IndexVec<VariantIdx, SourceInfo> = [
SourceInfo::outermost(body_span.shrink_to_lo()),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1894,7 +1894,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
let names = rib
.bindings
.iter()
.filter(|(id, _)| id.span.ctxt() == label.span.ctxt())
.filter(|(id, _)| id.span.eq_ctxt(label.span))
.map(|(id, _)| id.name)
.collect::<Vec<Symbol>>();

Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_span/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,9 @@ impl Span {
pub fn ctxt(self) -> SyntaxContext {
self.data_untracked().ctxt
}
pub fn eq_ctxt(self, other: Span) -> bool {
self.data_untracked().ctxt == other.data_untracked().ctxt
}
#[inline]
pub fn with_ctxt(self, ctxt: SyntaxContext) -> Span {
self.data_untracked().with_ctxt(ctxt)
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ symbols! {
Arc,
Argument,
ArgumentV1,
ArgumentV1Methods,
Arguments,
AsMut,
AsRef,
Expand Down Expand Up @@ -1641,7 +1642,7 @@ impl Ident {

impl PartialEq for Ident {
fn eq(&self, rhs: &Self) -> bool {
self.name == rhs.name && self.span.ctxt() == rhs.span.ctxt()
self.name == rhs.name && self.span.eq_ctxt(rhs.span)
}
}

Expand Down
1 change: 1 addition & 0 deletions library/core/src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ macro_rules! arg_new {
};
}

#[rustc_diagnostic_item = "ArgumentV1Methods"]
impl<'a> ArgumentV1<'a> {
#[doc(hidden)]
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
Expand Down
3 changes: 2 additions & 1 deletion library/core/src/ops/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ pub enum GeneratorState<Y, R> {
/// }
/// ```
///
/// More documentation of generators can be found in the unstable book.
/// More documentation of generators can be found in the [unstable book].
///
/// [RFC 2033]: https://github.com/rust-lang/rfcs/pull/2033
/// [unstable book]: ../../unstable-book/language-features/generators.html
#[lang = "generator"]
#[unstable(feature = "generator_trait", issue = "43122")]
#[fundamental]
Expand Down
21 changes: 21 additions & 0 deletions src/test/ui/consts/const-eval/format.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const fn failure() {
panic!("{:?}", 0);
//~^ ERROR cannot call non-const formatting macro in constant functions
//~| ERROR erroneous constant used
//~| ERROR erroneous constant used
//~| WARN this was previously accepted by the compiler
//~| WARN this was previously accepted by the compiler
}

const fn print() {
println!("{:?}", 0);
//~^ ERROR cannot call non-const formatting macro in constant functions
//~| ERROR `Arguments::<'a>::new_v1` is not yet stable as a const fn
//~| ERROR cannot call non-const fn `_print` in constant functions
//~| ERROR erroneous constant used
//~| ERROR erroneous constant used
//~| WARN this was previously accepted by the compiler
//~| WARN this was previously accepted by the compiler
}

fn main() {}
78 changes: 78 additions & 0 deletions src/test/ui/consts/const-eval/format.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
error[E0015]: cannot call non-const formatting macro in constant functions
--> $DIR/format.rs:2:20
|
LL | panic!("{:?}", 0);
| ^
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
= note: this error originates in the macro `$crate::const_format_args` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0015]: cannot call non-const formatting macro in constant functions
--> $DIR/format.rs:11:22
|
LL | println!("{:?}", 0);
| ^
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
= note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)

error: `Arguments::<'a>::new_v1` is not yet stable as a const fn
--> $DIR/format.rs:11:5
|
LL | println!("{:?}", 0);
| ^^^^^^^^^^^^^^^^^^^
|
= help: add `#![feature(const_fmt_arguments_new)]` to the crate attributes to enable
= note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0015]: cannot call non-const fn `_print` in constant functions
--> $DIR/format.rs:11:5
|
LL | println!("{:?}", 0);
| ^^^^^^^^^^^^^^^^^^^
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
= note: this error originates in the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)

error: erroneous constant used
--> $DIR/format.rs:2:12
|
LL | panic!("{:?}", 0);
| ^^^^^^ referenced constant has errors
|
= note: `#[deny(const_err)]` on by default
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>

error: erroneous constant used
--> $DIR/format.rs:2:20
|
LL | panic!("{:?}", 0);
| ^ referenced constant has errors
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
= note: this error originates in the macro `$crate::const_format_args` (in Nightly builds, run with -Z macro-backtrace for more info)

error: erroneous constant used
--> $DIR/format.rs:11:14
|
LL | println!("{:?}", 0);
| ^^^^^^ referenced constant has errors
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>

error: erroneous constant used
--> $DIR/format.rs:11:22
|
LL | println!("{:?}", 0);
| ^ referenced constant has errors
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
= note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 8 previous errors

For more information about this error, try `rustc --explain E0015`.