Skip to content

Commit 855eca0

Browse files
committed
Add constness qualifier to function signature
1 parent 008daf0 commit 855eca0

File tree

18 files changed

+79
-18
lines changed

18 files changed

+79
-18
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1296,7 +1296,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
12961296
}
12971297
}
12981298

1299-
fn lower_constness(&mut self, c: Const) -> hir::Constness {
1299+
pub(super) fn lower_constness(&mut self, c: Const) -> hir::Constness {
13001300
match c {
13011301
Const::Yes(_) => hir::Constness::Const,
13021302
Const::No => hir::Constness::NotConst,

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,6 +1247,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12471247
abi: this.lower_extern(f.ext),
12481248
decl: this.lower_fn_decl(&f.decl, None, false, None),
12491249
param_names: this.lower_fn_params_to_names(&f.decl),
1250+
constness: this.lower_constness(f.constness),
12501251
}))
12511252
})
12521253
}),

compiler/rustc_codegen_llvm/src/intrinsic.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,7 @@ fn gen_fn<'ll, 'tcx>(
683683
false,
684684
hir::Unsafety::Unsafe,
685685
Abi::Rust,
686+
hir::Constness::NotConst,
686687
));
687688
let fn_abi = FnAbi::of_fn_ptr(cx, rust_fn_sig, &[]);
688689
let llfn = cx.declare_fn(name, &fn_abi);
@@ -716,13 +717,15 @@ fn get_rust_try_fn<'ll, 'tcx>(
716717
false,
717718
hir::Unsafety::Unsafe,
718719
Abi::Rust,
720+
hir::Constness::NotConst,
719721
)));
720722
let catch_fn_ty = tcx.mk_fn_ptr(ty::Binder::bind(tcx.mk_fn_sig(
721723
[i8p, i8p].iter().cloned(),
722724
tcx.mk_unit(),
723725
false,
724726
hir::Unsafety::Unsafe,
725727
Abi::Rust,
728+
hir::Constness::NotConst,
726729
)));
727730
let output = tcx.types.i32;
728731
let rust_try = gen_fn(cx, "__rust_try", vec![try_fn_ty, i8p, catch_fn_ty], output, codegen);

compiler/rustc_hir/src/hir.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2035,6 +2035,7 @@ pub struct BareFnTy<'hir> {
20352035
pub generic_params: &'hir [GenericParam<'hir>],
20362036
pub decl: &'hir FnDecl<'hir>,
20372037
pub param_names: &'hir [Ident],
2038+
pub constness: Constness
20382039
}
20392040

20402041
#[derive(Debug, HashStable_Generic)]
@@ -2517,6 +2518,15 @@ pub enum Constness {
25172518
NotConst,
25182519
}
25192520

2521+
impl fmt::Display for Constness {
2522+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2523+
f.write_str(match *self {
2524+
Self::Const => "const",
2525+
Self::NotConst => ""
2526+
})
2527+
}
2528+
}
2529+
25202530
#[derive(Copy, Clone, Encodable, Debug, HashStable_Generic)]
25212531
pub struct FnHeader {
25222532
pub unsafety: Unsafety,

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ impl<'a> State<'a> {
404404
self.print_ty_fn(
405405
f.abi,
406406
f.unsafety,
407+
f.constness,
407408
&f.decl,
408409
None,
409410
&f.generic_params,
@@ -2310,6 +2311,7 @@ impl<'a> State<'a> {
23102311
&mut self,
23112312
abi: Abi,
23122313
unsafety: hir::Unsafety,
2314+
constness: hir::Constness,
23132315
decl: &hir::FnDecl<'_>,
23142316
name: Option<Symbol>,
23152317
generic_params: &[hir::GenericParam<'_>],
@@ -2330,7 +2332,7 @@ impl<'a> State<'a> {
23302332
hir::FnHeader {
23312333
unsafety,
23322334
abi,
2333-
constness: hir::Constness::NotConst,
2335+
constness,
23342336
asyncness: hir::IsAsync::NotAsync,
23352337
},
23362338
name,

compiler/rustc_middle/src/ty/context.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2082,7 +2082,7 @@ impl<'tcx> TyCtxt<'tcx> {
20822082
ty::Tuple(params) => params.into_iter().map(|k| k.expect_ty()),
20832083
_ => bug!(),
20842084
};
2085-
self.mk_fn_sig(params_iter, s.output(), s.c_variadic, unsafety, abi::Abi::Rust)
2085+
self.mk_fn_sig(params_iter, s.output(), s.c_variadic, unsafety, abi::Abi::Rust, hir::Constness::NotConst,)
20862086
})
20872087
}
20882088

@@ -2459,6 +2459,7 @@ impl<'tcx> TyCtxt<'tcx> {
24592459
c_variadic: bool,
24602460
unsafety: hir::Unsafety,
24612461
abi: abi::Abi,
2462+
constness: hir::Constness,
24622463
) -> <I::Item as InternIteratorElement<Ty<'tcx>, ty::FnSig<'tcx>>>::Output
24632464
where
24642465
I: Iterator<Item: InternIteratorElement<Ty<'tcx>, ty::FnSig<'tcx>>>,
@@ -2468,6 +2469,7 @@ impl<'tcx> TyCtxt<'tcx> {
24682469
c_variadic,
24692470
unsafety,
24702471
abi,
2472+
constness
24712473
})
24722474
}
24732475

compiler/rustc_middle/src/ty/error.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pub enum TypeError<'tcx> {
3636
Mismatch,
3737
UnsafetyMismatch(ExpectedFound<hir::Unsafety>),
3838
AbiMismatch(ExpectedFound<abi::Abi>),
39+
ConstnessMismatch(ExpectedFound<hir::Constness>),
3940
Mutability,
4041
TupleSize(ExpectedFound<usize>),
4142
FixedArraySize(ExpectedFound<u64>),
@@ -109,6 +110,9 @@ impl<'tcx> fmt::Display for TypeError<'tcx> {
109110
AbiMismatch(values) => {
110111
write!(f, "expected {} fn, found {} fn", values.expected, values.found)
111112
}
113+
ConstnessMismatch(values) => {
114+
write!(f, "expected {} fn, found {} fn", values.expected, values.found)
115+
}
112116
Mutability => write!(f, "types differ in mutability"),
113117
TupleSize(values) => write!(
114118
f,
@@ -197,9 +201,9 @@ impl<'tcx> TypeError<'tcx> {
197201
pub fn must_include_note(&self) -> bool {
198202
use self::TypeError::*;
199203
match self {
200-
CyclicTy(_) | CyclicConst(_) | UnsafetyMismatch(_) | Mismatch | AbiMismatch(_)
201-
| FixedArraySize(_) | Sorts(_) | IntMismatch(_) | FloatMismatch(_)
202-
| VariadicMismatch(_) | TargetFeatureCast(_) => false,
204+
CyclicTy(_) | CyclicConst(_) | UnsafetyMismatch(_) | Mismatch | AbiMismatch(_) | ConstnessMismatch(_) | FixedArraySize(_)
205+
| Sorts(_) | IntMismatch(_) | FloatMismatch(_) | VariadicMismatch(_)
206+
| TargetFeatureCast(_) => false,
203207

204208
Mutability
205209
| TupleSize(_)

compiler/rustc_middle/src/ty/layout.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2339,15 +2339,16 @@ impl<'tcx> ty::Instance<'tcx> {
23392339
let sig = substs.as_closure().sig();
23402340

23412341
let env_ty = tcx.closure_env_ty(def_id, substs).unwrap();
2342-
sig.map_bound(|sig| {
2343-
tcx.mk_fn_sig(
2344-
iter::once(env_ty.skip_binder()).chain(sig.inputs().iter().cloned()),
2345-
sig.output(),
2346-
sig.c_variadic,
2347-
sig.unsafety,
2348-
sig.abi,
2349-
)
2350-
})
2342+
2343+
sig.map_bound(|sig|
2344+
tcx.mk_fn_sig(
2345+
iter::once(env_ty.skip_binder()).chain(sig.inputs().iter().cloned()),
2346+
sig.output(),
2347+
sig.c_variadic,
2348+
sig.unsafety,
2349+
sig.abi,
2350+
hir::Constness::NotConst,
2351+
))
23512352
}
23522353
ty::Generator(_, substs, _) => {
23532354
let sig = substs.as_generator().poly_sig();
@@ -2373,6 +2374,7 @@ impl<'tcx> ty::Instance<'tcx> {
23732374
false,
23742375
hir::Unsafety::Normal,
23752376
rustc_target::spec::abi::Abi::Rust,
2377+
hir::Constness::NotConst,
23762378
)
23772379
})
23782380
}

compiler/rustc_middle/src/ty/relate.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ impl<'tcx> Relate<'tcx> for ty::FnSig<'tcx> {
165165
}
166166
let unsafety = relation.relate(a.unsafety, b.unsafety)?;
167167
let abi = relation.relate(a.abi, b.abi)?;
168+
let constness = relation.relate(a.constness, b.constness)?;
168169

169170
if a.inputs().len() != b.inputs().len() {
170171
return Err(TypeError::ArgCount);
@@ -189,6 +190,7 @@ impl<'tcx> Relate<'tcx> for ty::FnSig<'tcx> {
189190
c_variadic: a.c_variadic,
190191
unsafety,
191192
abi,
193+
constness
192194
})
193195
}
194196
}
@@ -217,6 +219,17 @@ impl<'tcx> Relate<'tcx> for abi::Abi {
217219
}
218220
}
219221

222+
impl<'tcx> Relate<'tcx> for ast::Constness {
223+
fn relate<R: TypeRelation<'tcx>>(
224+
relation: &mut R,
225+
a: ast::Constness,
226+
b: ast::Constness
227+
) -> RelateResult<'tcx, ast::Constness> {
228+
if a == b || (a == ast::Constness::Const && b == ast::Constness::NotConst) { Ok(a) }
229+
else { Err(TypeError::ConstnessMismatch(expected_found(relation, a, b))) }
230+
}
231+
}
232+
220233
impl<'tcx> Relate<'tcx> for ty::ProjectionTy<'tcx> {
221234
fn relate<R: TypeRelation<'tcx>>(
222235
relation: &mut R,

compiler/rustc_middle/src/ty/structural_impls.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,7 @@ impl<'a, 'tcx> Lift<'tcx> for ty::FnSig<'a> {
648648
c_variadic: self.c_variadic,
649649
unsafety: self.unsafety,
650650
abi: self.abi,
651+
constness: self.constness
651652
})
652653
}
653654
}
@@ -670,6 +671,7 @@ impl<'a, 'tcx> Lift<'tcx> for ty::error::TypeError<'a> {
670671
Mismatch => Mismatch,
671672
UnsafetyMismatch(x) => UnsafetyMismatch(x),
672673
AbiMismatch(x) => AbiMismatch(x),
674+
ConstnessMismatch(x) => ConstnessMismatch(x),
673675
Mutability => Mutability,
674676
TupleSize(x) => TupleSize(x),
675677
FixedArraySize(x) => FixedArraySize(x),

0 commit comments

Comments
 (0)