diff --git a/crates/nu-engine/src/eval_ir.rs b/crates/nu-engine/src/eval_ir.rs index 161addd641..674be96f75 100644 --- a/crates/nu-engine/src/eval_ir.rs +++ b/crates/nu-engine/src/eval_ir.rs @@ -436,7 +436,16 @@ fn eval_instruction( let val = ctx.collect_reg(*val, *span)?; let record_span = record_value.span(); let mut record = record_value.into_record()?; - record.insert(key.coerce_into_string()?, val); + + let key = key.coerce_into_string()?; + if let Some(old_value) = record.insert(&key, val) { + return Err(ShellError::ColumnDefinedTwice { + col_name: key, + second_use: *span, + first_use: old_value.span(), + }); + } + ctx.put_reg( *src_dst, Value::record(record, record_span).into_pipeline_data(), diff --git a/crates/nu-protocol/src/ir/mod.rs b/crates/nu-protocol/src/ir/mod.rs index e098b9e33e..a202c7b70c 100644 --- a/crates/nu-protocol/src/ir/mod.rs +++ b/crates/nu-protocol/src/ir/mod.rs @@ -150,8 +150,8 @@ pub enum Instruction { ListPush { src_dst: RegId, item: RegId }, /// Spread a value onto the end of a list. Used to construct list literals. ListSpread { src_dst: RegId, items: RegId }, - /// Insert a key-value pair into a record. Used to construct record literals. Any existing value - /// for the key is overwritten. + /// Insert a key-value pair into a record. Used to construct record literals. Raises an error if + /// the key already existed in the record. RecordInsert { src_dst: RegId, key: RegId,